Skip to content

Instantly share code, notes, and snippets.

@AndrewGuard
Forked from ksolo/form-validator.js
Last active December 25, 2015 19:59
Show Gist options
  • Save AndrewGuard/7031872 to your computer and use it in GitHub Desktop.
Save AndrewGuard/7031872 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 password = this.password.value;
var email = this.email.value
console.log(password);
var check_email = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
var check_length = /.{8,}/;
if(!password.match(check_length)) {
$("#error").append("yo password is short<br>");
}
if(!email.match(check_email)) {
$("#error").append("yo email is bust, biznitch<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><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