-
-
Save dustinfox-code/9257078 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(){ | |
//Your code... | |
$("form").on("submit",function(event){ | |
event.preventDefault(); | |
$('#errors').empty(); | |
var email = validateEmail(this['email'].value); | |
var password = validatePassword(this["password"].value); | |
if (email != true) | |
{ | |
$('#errors').append(email); | |
} | |
if (password != true) | |
{ | |
for (var i =0; i < password.length; i ++) | |
{ | |
$('#errors').append(password[i]); | |
} | |
} | |
}) | |
}); | |
function validateEmail(email) | |
{ | |
if(/^(([^<>()[\]\\.,;:\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,}))$/.test(email) || email == "" || email == " ") | |
{ | |
return true; | |
} | |
else | |
{ | |
return "<li>Must be a valie email</li>"; | |
} | |
} | |
function validatePassword(password) | |
{ | |
var password = password.value | |
var errors = []; | |
if (/.{8,}/.test(password) == false) | |
{ | |
errors.push("<li>The password must be at least 8 characters in length</li>"); | |
}; | |
if (/[A-Z]+/.test(password) == false) | |
{ | |
errors.push("<li>The password must contain at least 1 capital letter</li>"); | |
}; | |
if (/[0-9]+/.test(password) == false) | |
{ | |
errors.push("<li>The password must contain at least 1 number</li>"); | |
}; | |
if (errors == []) | |
{ | |
return true; | |
} | |
else | |
{ | |
return errors; | |
}; | |
} |
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