Skip to content

Instantly share code, notes, and snippets.

@tlands
Forked from ksolo/form-validator.js
Last active December 22, 2015 10:19
Show Gist options
  • Save tlands/6457801 to your computer and use it in GitHub Desktop.
Save tlands/6457801 to your computer and use it in GitHub Desktop.
Form Validation NEED TO REDO THIS OO STYLE ;)
$(document).ready(function() {
$.validator.addMethod("capLetsRegex", function(value) {
return /[A-Z]+/.test(value);
}, "Password must contain at least one capital letter.");
$.validator.addMethod("includeNumeric", function(value) {
return /[0-9]+/.test(value);
}, "Password must contain at least one number.");
$.validator.addMethod("emailRegex", function(value) {
return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value);
}, "Email must be valid.");
$('#signup').validate({
errorLabelContainer: '#errors',
wrapper: 'li',
onkeyup: false,
rules: {
email: {
emailRegex: true
},
password: {
required: true,
includeNumeric: true,
capLetsRegex: true,
minlength: 8
},
messages: {
email: {
emailRegex: "You must have a valid email."
},
password: {
required: "You need to have a password",
includeNumeric: "Your password needs at least one number.",
capLetsRegex: "Your password needs at least one capital letter.",
minlength: "Your password must be at least 8 characters long."
}
}
} // closing rules
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="main.css">
<title>Form Validation</title>
</head>
<body>
<form name="sign_up" action="#" method="post">
<label for="email">Email</label>
<input type="text" name="email" />
<label for="password">Password</label>
<input type="password" name="password" />
<button type="submit">Sign Up</button>
<ul id="errors"></ul>
</form><body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="form-validator.js"></script>
</body>
</html>
ul#errors {
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment