function testRegularExpression(regExp, value, emptyAllowed) { var emptyRegExp = "^$"; return emptyAllowed ? ( value.match(regExp) || value.match(emptyRegExp) ) : value.match(regExp); } function isNotEmpty(value) { return !value.match('^[ ]*$'); } function isValidSSN(value) { return dispatchSSN(value, false); } function _isValidSSN(value) { return dispatchSSN(value, true); } function dispatchSSN(value, emptyAllowed) { return testRegularExpression('^[0-9]{3}-[0-9]{2}-[0-9]{4}$', value, emptyAllowed); } function isValidAlpha(value) { return dispatchAlpha(value, false); } function _isValidAlpha(value) { return dispatchAlpha(value, true); } function dispatchAlpha(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z]+$', value, emptyAllowed); } function isValidName(value) { return dispatchName(value, false); } function _isValidName(value) { return dispatchName(value, true); } function dispatchName(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z0-9][ a-z0-9A-Z&,\\.\\-]*$', value, emptyAllowed); } function isValidAddress(value) { return dispatchAddress(value, false); } function _isValidAddress(value) { return dispatchAddress(value, true); } function dispatchAddress(value, emptyAllowed) { return testRegularExpression('^([Pp][\\. ]?[oO][\\. ]? *[bB][oO][xX])? *[1-9][0-9]*[ 0-9a-zA-Z#\\B/,-.]*$', value, emptyAllowed); } function isValidCity(value) { return dispatchCity(value, false); } function _isValidCity(value) { return dispatchCity(value, true); } function dispatchCity(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z]([ ]*[a-zA-Z]+)*$', value, emptyAllowed); } function isValidURL(value) { return dispatchURL(value, false); } function _isValidURL(value) { return dispatchURL(value, true); } function dispatchURL(value, emptyAllowed) { return testRegularExpression('^http(s)?://.+\\.[a-zA-Z0-9]{2,3}/?$', value, emptyAllowed); } function isValidEmail(value) { return dispatchEmail(value, false); } function _isValidEmail(value) { return dispatchEmail(value, true); } function dispatchEmail(value, emptyAllowed) { return testRegularExpression('^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$', value, emptyAllowed); } function isValidPhone(value) { return dispatchPhone(value, false); } function _isValidPhone(value) { return dispatchPhone(value, true); } function dispatchPhone(value, emptyAllowed) { return testRegularExpression('^[1-9][0-9]{2}-[1-9][0-9]{2}-[0-9]{4}$', value, emptyAllowed); } function isValidZip(value) { return dispatchZip(value, false); } function _isValidZip(value) { return dispatchZip(value, true); } function dispatchZip(value, emptyAllowed) { return testRegularExpression('^[0-9]{5}$', value, emptyAllowed); } function isValidYear(value) { return dispatchYear(value, false); } function _isValidYear(value) { return dispatchYear(value, true); } function dispatchYear(value, emptyAllowed) { return testRegularExpression('^[1-2][0-9]{3}$', value, emptyAllowed); } function isValidInteger(value) { return dispatchInteger(value, false); } function _isValidInteger(value) { return dispatchInteger(value, true); } function dispatchInteger(value, emptyAllowed) { return testRegularExpression('^[0-9]+$', value, emptyAllowed); } function isValidUsername(value) { return dispatchUsername(value, false); } function _isValidUsername(value) { return dispatchUsername(value, true); } function dispatchUsername(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z0-9][a-zA-Z0-9_]{3,}$', value, emptyAllowed); } function isValidPassword(value) { return dispatchPassword(value, false); } function _isValidPassword(value) { return dispatchPassword(value, true); } function dispatchPassword(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z0-9]{3,10}$', value, emptyAllowed); } function isValidState(value) { return dispatchState(value, false); } function _isValidState(value) { return dispatchState(value, true); } function dispatchState(value, emptyAllowed) { return testRegularExpression('^(CA|AL|AK|AZ|AR|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY|GU|PR|VI)$', value, emptyAllowed); } function isValidPersonStatus(value) { return dispatchPersonStatus(value, false); } function _isValidPersonStatus(value) { return dispatchPersonStatus(value, true); } function dispatchPersonStatus(value, emptyAllowed) { return testRegularExpression('^(active|inactive|pending)$', value, emptyAllowed); } function isValidAlphabetic(value) { return dispatchAlphabetic(value, false); } function _isValidAlphabetic(value) { return dispatchAlphabetic(value, true); } function dispatchAlphabetic(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z]+$', value, emptyAllowed); } function isValidAlphanumeric(value) { return dispatchAlphanumeric(value, false); } function _isValidAlphanumeric(value) { return dispatchAlphanumeric(value, true); } function dispatchAlphanumeric(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z0-9]+$', value, emptyAllowed); } function isValidNonNegativeNumeric(value) { return dispatchNonNegativeNumeric(value, false); } function _isValidNonNegativeNumeric(value) { return dispatchNonNegativeNumeric(value, true); } function dispatchNonNegativeNumeric(value, emptyAllowed) { return testRegularExpression('^[0-9]+$', value, emptyAllowed); } function isValidNegativeAllowedNumeric(value) { return dispatchNegativeAllowedNumeric(value, false); } function _isValidNegativeAllowedNumeric(value) { return dispatchNegativeAllowedNumeric(value, true); } function dispatchNegativeAllowedNumeric(value, emptyAllowed) { return testRegularExpression('^-?[0-9]+$', value, emptyAllowed); } function isValidNonNegativeFloat(value) { return dispatchNonNegativeFloat(value, false); } function _isValidNonNegativeFloat(value) { return dispatchNonNegativeFloat(value, true); } function dispatchNonNegativeFloat(value, emptyAllowed) { return testRegularExpression('^[0-9]*\\.?[0-9]+$', value, emptyAllowed); } function isValidNegativeAllowedFloat(value) { return dispatchNegativeAllowedFloat(value, false); } function _isValidNegativeAllowedFloat(value) { return dispatchNegativeAllowedFloat(value, true); } function dispatchNegativeAllowedFloat(value, emptyAllowed) { return testRegularExpression('-?[0-9]*\\.?[0-9]+$', value, emptyAllowed); } function isValidWCAB(value) { return dispatchWCAB(value, false); } function _isValidWCAB(value) { return dispatchWCAB(value, true); } function dispatchWCAB(value, emptyAllowed) { return testRegularExpression('^[A-Z]{3} [0-9]{7}$', value, emptyAllowed); } function isValidDigit(value) { return dispatchDigit(value, false); } function _isValidDigit(value) { return dispatchDigit(value, true); } function dispatchDigit(value, emptyAllowed) { return testRegularExpression('^[0-9]$', value, emptyAllowed); } function isValidBoolean(value) { return dispatchBoolean(value, false); } function _isValidBoolean(value) { return dispatchBoolean(value, true); } function dispatchBoolean(value, emptyAllowed) { return testRegularExpression('^([tT][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|1|0)$', value, emptyAllowed); } function isValidExtension(value) { return dispatchExtension(value, false); } function _isValidExtension(value) { return dispatchExtension(value, true); } function dispatchExtension(value, emptyAllowed) { return testRegularExpression('^[a-zA-Z0-9]+$', value, emptyAllowed); } function isValidPercentage(value) { return dispatchPercentage(value, false); } function _isValidPercentage(value) { return dispatchPercentage(value, true); } function dispatchPercentage(value, emptyAllowed) { return testRegularExpression('^((0?[0-9])|([1-9][0-9])|(100))$', value, emptyAllowed); } function isValidPaymentMethod(value) { return dispatchPaymentMethod(value, false); } function _isValidPaymentMethod(value) { return dispatchPaymentMethod(value, true); } function dispatchPaymentMethod(value, emptyAllowed) { return testRegularExpression('^(cash|credit|check)$', value, emptyAllowed); } function isValidCreditCardNumber(value) { return (testRegularExpression('^4[0-9]{3}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$', value, false)||testRegularExpression('^6011-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$', value, false)||testRegularExpression('^3[4,7][0-9]{13}$', value, false)||testRegularExpression('^5[1-5][0-9]{2}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$', value, false)) && luhn_check(value); } function isValidCVVNumber(value) { return (testRegularExpression('^[0-9]{3,4}$', value, false)); } function luhn_check(card_number) { cc_array = card_number.split( "" ); cc_array.reverse(); digit_string = " "; for ( counter=0; counter < cc_array.length; counter++ ) { current_digit = parseInt( cc_array[counter] ); if (counter %2 != 0) { cc_array[counter] *= 2; } digit_string += cc_array[counter]; } var current_digit, digit_sum = 0; for ( counter=0; counter < digit_string.length; counter++ ) { digit_sum += parseInt( digit_string.charAt(counter) ) ? parseInt( digit_string.charAt(counter) ) : 0; } if ( digit_sum % 10 == 0 ) { return true; } else { return false; } }