Skip to content

Instantly share code, notes, and snippets.

@MarkDucommun
Forked from ksolo/form-validator.js
Last active December 21, 2015 03:49
Show Gist options
  • Save MarkDucommun/6244931 to your computer and use it in GitHub Desktop.
Save MarkDucommun/6244931 to your computer and use it in GitHub Desktop.
Form Validation
$(function(){
$('button').click(function(event){
event.preventDefault();
var value = $('form').serializeArray();
console.log("stuff")
email = value[0].value
password = value[1].value
var errors = []
if(!value[0].value.match(/\w+@\w+[.]\w{3}/)){
errors.push("Must be a valid email address")
}
if(!value[1].value.match(/\w*[0-9]\w*/)){
errors.push("Password must have at least one numeric character (0-9)")
}
if(!value[1].value.match(/\w*[A-Z]\w*/)){
errors.push("Password must have at least one capital letter")
}
if(value[1].value.length < 8){
errors.push("Password must be at least 8 letters long")
}
if(errors === []){
$.post('#', {email: email, password: password})
}
else{
for (error in errors){
var li = "<li>" + errors[error] + "</>"
$('#errors').append(li);
}
}
});
});
// var valid_email = function(){
// email
// }
<!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