Skip to content

Instantly share code, notes, and snippets.

@HarishChaudhari
Last active March 20, 2017 14:46
Show Gist options
  • Save HarishChaudhari/a86c6cacc23974bb63e5 to your computer and use it in GitHub Desktop.
Save HarishChaudhari/a86c6cacc23974bb63e5 to your computer and use it in GitHub Desktop.
/**
* Form Validation
*
* @since 11/04/2014
* @author Harish
*/
function enformValidation() {
var i = 0;
// Validate
var err_flag = new Array();
err_flag[i] = enRegexValidator('#fname_home', 'regex', 'name', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#lname_home', 'regex', 'name', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#mname_home', 'null', 'name', 'allow_null'); i = i + 1;
err_flag[i] = enRegexValidator('#address1_home', 'regex', 'address', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#address2_home', 'null', 'address', 'allow_null'); i = i + 1;
err_flag[i] = enRegexValidator('#phone_home', 'regex', 'phone', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#mobphone_home', 'null', 'phone', 'allow_null'); i = i + 1;
err_flag[i] = enRegexValidator('#email_home', 'regex', 'email', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#city_home', 'regex', 'name', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#state_home', 'regex', 'name', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#zipcode_home', 'regex', 'zip', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#county_home', 'regex', 'name', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#bank-institution-name', 'regex', 'name', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#bank-institution-loc', 'regex', 'address', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#acc-number', 'regex', 'number', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#acc-routing-number', 'regex', 'number', 'not_null'); i = i + 1;
err_flag[i] = enRegexValidator('#dob_dep', 'regex', 'date', 'not_null'); i = i + 1;
// Failed, so stop JS from proceeding
if( jQuery.inArray("f", err_flag) !== -1 ) {
alert('There are validation errors in this step.');
exit;
}
}
/**
* RegEx Validator
*
* @since 11/04/2014
* @author Harish
*/
function enRegexValidator(id, validation_type, regex_type, checknull) {
if( jQuery(id).length > 0) {
var value = jQuery(id).val();
var check_regex_for_this = false
if( validation_type == 'null' ) {
// Fist check for NULL
switch( checknull ) {
case 'allow_null':
var is_null = true;
break;
case 'not_null':
case '':
default:
var is_null = false;
break;
}
jQuery(id).parent().find('.en-err-msg').remove();
if( !is_null && value.length == 0 ) { // Failed
jQuery(id).parent().append('<span class="en-err-msg">This field is required</span>');
jQuery(id).addClass('error').addClass('en-error-border');
return 'f';
} else if( !is_null && value.length > 0 ) { // Success for not_null elements
jQuery(id).parent().find('.en-err-msg').remove();
jQuery(id).removeClass('error').removeClass('en-error-border');
return 'y';
} else if( is_null && value.length == 0 ) { // Success for allow_null elements
jQuery(id).parent().find('.en-err-msg').remove();
jQuery(id).removeClass('error').removeClass('en-error-border');
return 'y';
}
}
if( (validation_type == 'regex' || validation_type == 'null') ) {
// RegEx will not be validated for elements allowing NULL(not required)
// Select the RegEx
switch( regex_type ) {
case 'name':
//var reg = /^[a-zA-Z]+$/;
var reg = /^([a-zA-Z \-.\'.\_]+)$/;
break;
case 'address':
var reg = /^([a-zA-Z0-9 \(\)\#\&.\,.\-.\'.\_]+)$/;
break;
case 'number':
var reg = /^([0-9]+)$/;
break;
case 'phone':
var reg = /^(?:\([0-9]\d{2}\)\ ?|(?:[0-9]\d{2}\-))[0-9]\d{2}\-\d{4}$/;
break;
case 'zip':
var reg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
break;
case 'ssn':
var reg = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
break;
case 'fssn':
var reg = /^\d{3,}$/;
break;
case 'sssn':
var reg = /^\d{2,}$/;
break;
case 'tssn':
var reg = /^\d{4,}$/;
break;
case 'date':
var reg = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
break;
case 'email':
var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
break;
case '':
default:
var reg = false;
break;
}
if( reg ) {
// Test the RegEx
jQuery(id).parent().find('.en-err-msg').remove();
if (!reg.test(value)) {
if( value.length == 0 ) {
jQuery(id).parent().append('<span class="en-err-msg">Please fill this field</span>');
} else {
jQuery(id).parent().append('<span class="en-err-msg">Inccorrect format</span>');
}
jQuery(id).addClass('error').addClass('en-error-border');
return 'f';
} else {
// Success
jQuery(id).parent().find('.en-err-msg').remove();
jQuery(id).removeClass('error').removeClass('en-error-border');
return 'y';
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment