Skip to content

Instantly share code, notes, and snippets.

@anirudh-eka
Forked from ksolo/form-validator.js
Last active December 21, 2015 03:58
Show Gist options
  • Save anirudh-eka/6245586 to your computer and use it in GitHub Desktop.
Save anirudh-eka/6245586 to your computer and use it in GitHub Desktop.
Form Validation
// shorthand for $(document).ready();
$(function(){
$('form').on('submit', function(e){
e.preventDefault();
var email_regex = new RegExp("[a-zA-Z]+[@][a-zA-Z]+[.][a-z]{2,}");
var email_input = $('input[type=text]').val();
var email_match = email_regex.test(email_input);
if (email_match == false){
$('#errors').append('<li>Must be a valid email address</li>');
};
var pw_input = $('input[type=password]').val();
var pw_check_one = /\d{1,}/
var pw_check_two = /[A-Z]{1,}/
var pw_check_three = /[\d\w]{8,}/
if (pw_check_one.test(pw_input) == false) {
$('#errors').append('<li>Password must have at least one numeric character(0-9)</li>');
};
if (pw_check_two.test(pw_input) == false) {
$('#errors').append('<li>Password must have at least one capital letter</li>');
};
if (pw_check_three.test(pw_input) == false) {
$('#errors').append('<li>Password must be at least 8 characters long</li>');
};
});
});
<!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;
}
@georgepradhan
Copy link

nice

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