-
-
Save kmazanec/6718398 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 verifyEmail (email) { | |
var em_re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
return em_re.test(email); | |
} | |
$(function(){ | |
$("form").on("submit", function(event){ | |
$('#errors').empty(); | |
if (! verifyEmail(event.target.email.value)) { | |
$('#errors').append("<li>Must be a valid email address</li"); | |
event.preventDefault(); | |
} | |
if (!(/.*\d.*/.test(event.target.password.value))) { | |
$('#errors').append("<li>Password must include at least one digit</li"); | |
event.preventDefault(); | |
} | |
if (!(/.*[A-Z].*/.test(event.target.password.value))) { | |
$('#errors').append("<li>Password must include at least one capital letter</li"); | |
event.preventDefault(); | |
} | |
if (event.target.password.value.length < 8) { | |
$('#errors').append("<li>Password must be at least 8 characters long</li"); | |
event.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