-
-
Save joelevering/7029677 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(e){ | |
//Your code... | |
function appendError(message){ | |
$("#errors").append("<li>" + message + "</li>"); | |
} | |
$('form').on("submit", function(e) { | |
var email = $('input[name=email]').val(); | |
var password = $('input[name=password]').val(); | |
var test_array = []; | |
var emailRegEx = /.+@.+\.(.{2,3})/; | |
var numPass = /(.*)\d(.*)/; | |
var capPass = /(.*)[A-Z](.*)/; | |
var lengthPass = /.{8,}/; | |
if(emailRegEx.test(email) === false) | |
{ | |
appendError("Enter a valid email."); | |
e.preventDefault(); | |
} | |
if (numPass.test(password) === false) | |
{ | |
appendError("Include a number in your password."); | |
e.preventDefault(); | |
} | |
if (capPass.test(password) === false) | |
{ | |
appendError("Include a capital letter in your password."); | |
e.preventDefault(); | |
} | |
if (lengthPass.test(password) === false) | |
{ | |
appendError("Password must be at least 8 characters long."); | |
e.preventDefault(); | |
} | |
}); | |
}); |
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