-
-
Save To-mos/9253687 to your computer and use it in GitHub Desktop.
Form Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// shorthand for $(document).ready(); | |
$(function() { | |
//Your code... | |
$('form[name="sign_up"]').submit(function(e) { | |
e.preventDefault(); | |
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; | |
var email = $('input[name="email"]').val(); | |
var password = $('input[name="password"]').val(); | |
var errorsOut = ""; //concatenation baby | |
if (email.length == 0) | |
errorsOut += '<li>Email can\'t be empty</li>'; | |
if (!emailReg.test(email)) | |
errorsOut += '<li>Must be a valid email address</li>'; | |
if (!/\d/.test(password)) | |
errorsOut += '<li>Password must have at least one numeric character (0-9)</li>'; | |
if (!/[A-Z]/.test(password)) | |
errorsOut += '<li>Password must have at least one capital letter</li>'; | |
if (password.length < 8) | |
errorsOut += '<li>Password must be at least 8 characters long</li>' | |
$('#errors').html(errorsOut); | |
if (errorsOut == "") | |
this.submit(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ul#errors { | |
color: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment