-
-
Save wiltonmaddams/9260063 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(){ | |
$("form").on('submit', function(event) { | |
event.preventDefault(); | |
checkEmail($("input[name='email']").val()); | |
checkPassword($("input[name='password']").val()); | |
}); | |
}); | |
function checkEmail(email) { | |
var emailRegEx = /.+@\w{1,}.\w{2,}/; | |
var email_pass_result = emailRegEx.test(email); | |
if(email_pass_result === false) { | |
$('#errors').append('<li>Must be a valid email address</li>'); | |
} | |
}; | |
function checkPassword(password) { | |
$('#errors').empty(); | |
checkPasswordCapitals(password); | |
checkPasswordLength(password); | |
checkPasswordNumeric(password); | |
}; | |
function checkPasswordNumeric(password) { | |
var numericRegEx = /[0-9]+/; | |
var numberPassResult = numericRegEx.test(password); | |
if(numberPassResult === false) { | |
$('#errors').append('<li>Must contain at least one numeric character (0-9)</li>'); | |
} | |
} | |
function checkPasswordCapitals(password) { | |
var capitalRegEx = /[A-Z]+/; | |
var capitalPassResult = capitalRegEx.test(password); | |
if(capitalPassResult === false) { | |
$('#errors').append('<li>Must contain at least one capital letter</li>'); | |
} | |
} | |
function checkPasswordLength(password) { | |
var lengthRegEx = /.{8,}/; | |
var lengthPassResult = lengthRegEx.test(password); | |
if(lengthPassResult === false) { | |
$('#errors').append('<li>Must be at least eight characters</li>'); | |
} | |
} |
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