Skip to content

Instantly share code, notes, and snippets.

@stevenspiel
Forked from ksolo/form-validator.js
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenspiel/8849727 to your computer and use it in GitHub Desktop.
Save stevenspiel/8849727 to your computer and use it in GitHub Desktop.
Form Validation
$(function(){
$('#submit').click(function(e){
e.preventDefault();
var errors = [];
if(!$('#email').val().match(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i) ){
errors.push("<li>Must be a valid email address </li>");
}
if(!$('#password').val().match(/[0-9]+?/)){
errors.push("<li>Password must have at least one numeric character (0-9)</li>");
}
if(!$('#password').val().match(/[A-Z]+?/)){
errors.push("<li>Password must have at least one capital letter</li>");
}
if(!$('#password').val().match(/.{8}/)){
errors.push("<li>Password must be at least 8 characters long</li>");
}
$('#errors').html(errors);
})
});
<!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" id="sign-up" action="#" method="post">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
<label for="password">Password</label>
<input type="password" name="password" id="password" />
<button type="submit" id="submit">Sign Up</button>
</form>
<ul id="errors"></ul>
<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