Last active
August 31, 2016 10:08
-
-
Save nothingatalldotnet/089a9e88196c12a9fd39dfa74b016e64 to your computer and use it in GitHub Desktop.
Simple universal form validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the password validator to match the following rules: