Skip to content

Instantly share code, notes, and snippets.

@nothingatalldotnet
Last active August 31, 2016 10:08
Show Gist options
  • Save nothingatalldotnet/089a9e88196c12a9fd39dfa74b016e64 to your computer and use it in GitHub Desktop.
Save nothingatalldotnet/089a9e88196c12a9fd39dfa74b016e64 to your computer and use it in GitHub Desktop.
Simple universal form validator
validator: function(form) {
var valid_form = true,
password_array = [],
reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{16,}$/;
$(form+" *").filter(':input').each(function(){
switch ($(this).attr('type')) {
case 'text':
if($(this).val() !== "") {
if($.trim($(this).val()) === "") {
valid_form = false;
$(this).addClass('error');
}
} else {
valid_form = false;
$(this).addClass('error');
}
break;
case 'password':
password_array.push($(this));
if($(this).val() !== "") {
if($.trim($(this).val()) === "") {
valid_form = false;
$(this).addClass('error');
} else {
}
} else {
valid_form = false;
$(this).addClass('error');
}
break;
case 'email':
if($(this).val() !== "") {
if($.trim($(this).val()) === "") {
valid_form = false;
$(this).addClass('error');
} else {
if(!(General.emailValidate($(this).val()))) {
valid_form = false;
$(this).addClass('error');
}
}
} else {
valid_form = false;
$(this).addClass('error');
}
break;
case 'checkbox' :
if(!($(this).prop('checked'))) {
valid_form = false;
$(this).addClass('error');
$(this).next().addClass('error');
}
break;
default:
}
});
if(password_array.length > 0) {
if(valid_form) {
if($(password_array[0]).val() !== $(password_array[1]).val()) {
valid_form = false;
$(password_array[0]).addClass('error');
$(password_array[1]).addClass('error');
} else {
if(!reg.test($(password_array[0]).val())) {
valid_form = false;
$(password_array[0]).addClass('error');
$(password_array[1]).addClass('error');
}
}
}
}
@nothingatalldotnet
Copy link
Author

Updated the password validator to match the following rules:

  • Minimum 16 characters
  • Must contain 1 number
  • Must contain 1 lowercase letter
  • Must contain 1 uppercase letter
  • Must contain 1 special character

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment