Skip to content

Instantly share code, notes, and snippets.

@eisenachalex
Forked from ksolo/form-validator.js
Last active December 25, 2015 19:49
Show Gist options
  • Save eisenachalex/7030115 to your computer and use it in GitHub Desktop.
Save eisenachalex/7030115 to your computer and use it in GitHub Desktop.
Form Validation
// shorthand for $(document).ready();
$(function(){
$("form").submit(function(e) {
e.preventDefault();
$("#error").html("");
var email = this.email.value;
var password = this.password.value;
var emailreg = /.+@.+\..+/;
var passwordlength = /.{8,}/
var passwordcaps = /[A-Z]+/
var passwordnumber = /\d+/
console.log(email);
if(!email.match(emailreg)) {
$("#error").append("Must be a valid email<br/>");
}
if(!password.match(passwordlength)) {
$("#error").append("Password must be 8 characters long<br/>");
}
if(!password.match(passwordcaps)) {
$("#error").append("Password must have at least one capital letter<br/>");
}
if(!password.match(passwordnumber)) {
$("#error").append("Password must have at least one numeric character(0-9)<br/>");
}
});
});
<!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>
<div id="error" style="color:red" >
</div>
<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