Skip to content

Instantly share code, notes, and snippets.

@eugenefilimonov
Forked from ksolo/form-validator.js
Last active December 22, 2015 09:49
Show Gist options
  • Save eugenefilimonov/6454173 to your computer and use it in GitHub Desktop.
Save eugenefilimonov/6454173 to your computer and use it in GitHub Desktop.
Form Validation
$(document).ready(function(){
$('form').on('submit',function(event){
event.preventDefault();
var email = $('input').first().val();
console.log(email);
var password = $('input').last().val();
console.log(password);
if (!emailValidate(email)){
$('#errors').append("<li> Must be a valid email address </li>");
};
var errors = passwordValidate(password);
if (errors != []) {
for (var i = 0;i<errors.length;i++){
$('#errors').append("<li>"+errors[i]+"</li>")
};
};
});
});
var emailValidate = function (email){
valid = email.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/)
if (valid != null) {
return true
}
else {
return false
}
};
var passwordValidate = function (password) {
var errors =[]
var checkForDigits = new RegExp("[1-9]+");
var checkforCapital = new RegExp('[A-Z]+');
if (password.length < 8) {
errors.push("Password must be at least 8 characters long.");
}
if (!checkForDigits.test(password)) {
errors.push("Password must have at least one numberic character (0-9).");
}
if (!checkforCapital.test(password)) {
errors.push("Password must have at least one capital letter.");
}
return errors
}
// console.log(passwordValidate("eu12"));
// console.log(emailValidate("eugene"));
<!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>
ul#errors {
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment