﻿
/**
* Valdiate object's value using specified parameters
*
* @param	textvalue	Text value to validate
* @param	minlen		Minimum length allowed for object's value
* @param	maxlen		Maximum length allowed for object's value
* @param	alpha		Must be alpha characters only [a-zA-Z]
* @param	numeric		Must be numeric characters only [0-9]
* @param	other		Custom regular expression
* @return				True or False if object is valid
*
*/
function textValidator_VC(textvalue, minlen, maxlen, alpha, numeric, other) {
    // if the length is out of bounds
    if (textvalue.length < minlen || textvalue.length > maxlen) {
        return false;
    }

    // optional fields allow lengths of zero
    if (textvalue.length != 0) {

        // check if alpha characters are allowed. if so:
        if (alpha && !numeric && !testAlpha(textvalue)) {
            return false;
        }
        // check if numerics are allowed. if so:
        if (numeric && !alpha && !testNumeric(textvalue)) {
            return false;
        }

        // check if alphanumeric is allowed
        if (numeric && alpha && !testAlphaNumeric(textvalue)) {
            return false;
        }
        // regexp
        if (other && !other.test(textvalue)) {
            return false;
        }
    }

    return true;
}

/**
* Test string if it contains alphabetical characters and spaces
* Note: considers "." to be valid character
*
* @param	word		String to be tested
* @return				True or False if string is valid
*
*/
function testAlpha(word) {
    var regexp = /^([a-zA-Z\s.])+$/;
    var blankexp = /^([\s])+$/;
    return ((regexp.test(word)) && (!blankexp.test(word)));
}

/**
* Test string if it contains numerical characters
*
* @param	word		String to be tested
* @return				True or False if string is valid
*
*/
function testNumeric(word) {
    var regexp = /^([0-9])+$/;
    return regexp.test(word);
}

/**
* Test string if it contains alphanumerical characters
* Note: considers "." to be valid character
*
* @param	word		String to be tested
* @return				True or False if string is valid
*
*/
function testAlphaNumeric(word) {
    var regexp = /^([0-9a-zA-Z\s.])+$/;
    var blankexp = /^([\s])+$/;
    return ((regexp.test(word)) && (!blankexp.test(word)));
}
