-
-
Save JTFrancis/6454784 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
function validatesEmail(email) { | |
var re = new RegExp ("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"); | |
if (re.test(email)) { | |
return true | |
} | |
else { | |
$('#errors').append('<li>Must be a valid email address</li>'); | |
return false | |
} | |
} | |
function validatesPassword(password) { | |
var pass = true | |
if (/\d/.test(password) == false) { | |
$('#errors').append('<li>Password must have at least one numeric character (0-9)</li>') | |
pass = false | |
} | |
if (/[A-Z]/.test(password) == false) { | |
$('#errors').append('<li>Password must have at least one capital letter</li>'); | |
} | |
if (password.length < 8) { | |
$('#errors').append('<li>Password must be at least 8 characters long</li>') | |
pass = false | |
} | |
return pass | |
} | |
// shorthand for $(document).ready(); | |
$(function(){ | |
//Your code... | |
$('form').submit(function(event) { | |
event.preventDefault(); | |
$('#errors').html('<span></span>'); | |
var email = $('input:nth-child(2)').val(); | |
var password = $('input:nth-child(4)').val(); | |
var email_valid = validatesEmail(email); | |
var pass_valid = validatesPassword(password); | |
if (email_valid && pass_valid) { | |
$.post('#'); | |
} | |
}); | |
}); |
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