-
-
Save mehagel/6243573 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
//Refactored | |
var errors = []; | |
var email_verification = function(){ | |
var x=document.forms["sign_up"]["email"].value; | |
var atpos=x.indexOf("@"); | |
var dotpos=x.lastIndexOf("."); | |
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { | |
errors.push("Must be a valid e-mail address."); | |
} | |
}; | |
var password_verification = function(){ | |
var p = document.forms["sign_up"]['password'].value; | |
var length = p.length; | |
var capital_pattern = /^(?=.*[A-Z])/; | |
var numeric_pattern = /^(?=.*\d)/; | |
if (length < 8) { | |
errors.push("Password must be at least 8 characters long."); | |
} | |
if (! p.match(capital_pattern)) { | |
errors.push("Password must contain at least 1 capital letter."); | |
} | |
if (! p.match(numeric_pattern)) { | |
errors.push("Password must contain at least 1 numeric character (0-9)."); | |
} | |
}; | |
$(function(){ | |
$('form').on('click', function(e) { | |
e.preventDefault(); | |
email_verification(); | |
password_verification(); | |
var newHTML = []; | |
for (var i = 0; i < errors.length; i++){ | |
newHTML.push('<li>' + errors[i] + '</li>'); | |
} | |
$('#errors').html(newHTML); | |
}); | |
}); |
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