-
-
Save rserna2010/6453410 to your computer and use it in GitHub Desktop.
Form Validation
This file contains hidden or 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(e){ | |
e.preventDefault(); | |
var email_input = $("form").find("input[name=email]").val() | |
var password_input = $("form").find("input[name=password]").val() | |
if (validateEmail(email_input)){ | |
console.log("success") | |
} | |
else { | |
$("#errors1").html("Must be a valid email address") | |
} | |
if (validatePasswordDigit(password_input)){ | |
console.log("success") | |
} else { | |
$("#errors2").html("Password must contain at least 1 digit between 0-9") | |
} | |
if (validatePasswordCaps(password_input)){ | |
console.log("success") | |
} else { | |
$("#errors3").html("Password must contain at least 1 capital letter") | |
} | |
if (validatePasswordLength(password_input)== true){ | |
console.log("success") | |
} else { | |
$("#errors4").html("Password must be at least 8 characters long") | |
} | |
}); | |
}); | |
function validateEmail(email) { | |
var email_regex = /^\S+@\S+$/; | |
return email_regex.test(email); | |
} | |
function validatePasswordDigit(password) { | |
var password_digit_regex = /[0-9]/; | |
return password_digit_regex.test(password); | |
} | |
function validatePasswordCaps(password) { | |
var password_caps_regex = /[A-Z]/; | |
return password_caps_regex.test(password); | |
} | |
function validatePasswordLength(password) { | |
var password_length_regex = /\S{8,}/; | |
return password_length_regex.test(password); | |
} |
This file contains hidden or 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="errors1"></ul> | |
<ul id="errors2"></ul> | |
<ul id="errors3"></ul> | |
<ul id="errors4"></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 hidden or 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 { | |
color: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment