-
-
Save gsperka/8874754 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(){ | |
$("button").click(function(e){ | |
e.preventDefault(); | |
var email = $("#email").val(); | |
var password = $("#password").val(); | |
checkError(email, password); | |
}); | |
}); | |
var checkError = function(email, password){ | |
$("#errors").empty(); | |
validateEmail(email); | |
validatePasswordLength(password); | |
validateCapital(password); | |
validateNumber(password); | |
if ($("#errors li").length === 0){ | |
$("#errors").append("<li>Valid Email and Password</li>"); | |
} | |
} | |
var validateEmail = function(email) { | |
var validFormat = /^(([^<>()[\]\\.,;:\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,}))$/; | |
if (validFormat.test(email)){ | |
return true; | |
} | |
else{ | |
$("#errors").append("<li>Must be a valid email address</li>"); | |
return false; | |
} | |
} | |
var validatePasswordLength = function(password){ | |
if(password.length < 8){ | |
$("#errors").append("<li>Password must be at least 8 characters long.</li>"); | |
return false; | |
} | |
else{ | |
return true; | |
} | |
} | |
var validateCapital = function(password){ | |
var validFormat = /[A-Z]/; | |
if (validFormat.test(password)){ | |
return true; | |
} | |
else{ | |
$("#errors").append("<li>Password must have at least one capital letter.</li>"); | |
return false; | |
} | |
} | |
var validateNumber = function(password){ | |
var validFormat = /[0-9]/; | |
if (validFormat.test(password)){ | |
return true; | |
} | |
else{ | |
$("#errors").append("<li>Password must have at least one numeric character(0-9).</li>"); | |
return false; | |
} | |
} |
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 id ="email" type="text" name="email" /> | |
<label for="password">Password</label> | |
<input id ="password" type="password" name="password" /> | |
<button id="click" 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> | |
<h2 id="resultA"></h2> | |
<h2 id="resultB"></h2> | |
</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
body { | |
text-align: center; | |
} | |
#errors { | |
list-style-type: none; | |
font-size: 24px; | |
color: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment