-
-
Save bahrieinn/6454118 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(); | |
$(document).ready(function(){ | |
$("form").submit(function(e) { | |
e.preventDefault(); | |
var emailInput = $("input").first().val(); | |
var passwordInput = $("input").last().val(); | |
if (!validateEmail(emailInput)) { | |
$("#errors").append("<li>Invalid Email</li>"); | |
} | |
var errors = validatePassword(passwordInput); | |
if (errors.length > 0 ) { | |
for (var i=0; i < errors.length; i++) { | |
$("#errors").append("<li>" + errors[i] + "</li>"); | |
} | |
} | |
}); | |
}); | |
var validateEmail = function(address) { | |
if (address.match(/(.+@\w+\.\w{2,3})/)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
var validatePassword = function(password) { | |
var errors = []; | |
if (!password.match(/[0-9]+/)) { | |
errors.push("Email must contain at least one number"); | |
} | |
if (!password.match(/[A-Z]+/)) { | |
errors.push("Email must contain at least one capital letter"); | |
} | |
if (password.length < 8) { | |
errors.push("Email must be at least 8 characters long") | |
} | |
return errors; | |
} | |
//Need to validate: | |
// 1. Email Format | |
// 2. Password: | |
// - one numeric character | |
// - one capital letter | |
// - at least 8 chars long | |
$("#errors").append() |
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