Skip to content

Instantly share code, notes, and snippets.

@scarow
Forked from ksolo/form-validator.js
Created September 5, 2013 22:29
Show Gist options
  • Save scarow/6457141 to your computer and use it in GitHub Desktop.
Save scarow/6457141 to your computer and use it in GitHub Desktop.
Form Validation
// shorthand for $(document).ready();
$(function(){
jQuery.validator.addMethod('mypassword', function(value, element) {
return this.optional(element) || (value.match(/[A-Z]/) && value.match(/[0-9]/));
},
'Password must contain at least one capital and one number.'
);
// $.validator.classRuleSettings.mypassword = {mypassword: true};
$('#signup').validate({
errorLabelContainer: "#errors",
wrapper: "li",
rules: {
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 8
},
messages: {
email: {
email: "Please enter valid email address"
},
password: {
minlength: "Password must be at least 8 characters"
}
}
}
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="main.css">
<title>Form Validation</title>
</head>
<body>
<form id="signup" name="sign_up" action="#" method="post">
<label for="email">Email</label>
<input type="text" name="email" />
<label for="password">Password</label>
<input type="password" class="require mypassword " 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="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.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