Skip to content

Instantly share code, notes, and snippets.

@Lydiakoller
Forked from ksolo/form-validator.js
Last active August 29, 2015 13:56
Show Gist options
  • Save Lydiakoller/8852287 to your computer and use it in GitHub Desktop.
Save Lydiakoller/8852287 to your computer and use it in GitHub Desktop.
Form Validation
$(function(){
$('#submit').click(function(e){
e.preventDefault();
var emailReg = /[\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/
var capitalReg = /[A-Z]{1,}/
var at_leastReg = /.{8,}/
var one_numReg = /\d{1,}/
var errors = [];
if(!$("#email").val().match(emailReg)){
errors.push("<li>email error</li>");
}
if(!$("#password").val().match(capitalReg)){
errors.push("<li>no capital letter</li>");
}
if(!$("#password").val().match(at_leastReg)){
errors.push("<li>must have at least 8 characters</li>");
}
if(!$("#password").val().match(one_numReg)){
errors.push("<li>must have one num</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" 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