Skip to content

Instantly share code, notes, and snippets.

@juandefelix
Forked from ksolo/form-validator.js
Last active March 5, 2020 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juandefelix/8854983 to your computer and use it in GitHub Desktop.
Save juandefelix/8854983 to your computer and use it in GitHub Desktop.
Form Validation
$(function(){
$("#target").submit(function(event){
event.preventDefault();
email = $("input[name='email']").val();
password = $("input[name='password']").val();
$('li').remove();
if (password.match(/\w{8}/) == null){
$("#errors").append("<li>Password must be at least 8 characters</li>");
};
if (password.match(/\d/) == null){
$("#errors").append("<li>Password must have at least one numeric charecter (0-9)</li>");
};
if (password.match(/[A-Z]/) == null){
$("#errors").append("<li>Password must have at least one capital letter</li>");
};
if (email.match(/\w+@\w+\.\w+/) == null){
$("#errors").append("<li>Must be a valid email address</li>");
};
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="main.css">
<title>Form Validation</title>
</head>
<body>
<form id="target" 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