-
-
Save nathanallen/6453695 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
$(function(){ | |
$('form').on('submit', function(e){ | |
e.preventDefault(); | |
$('#errors').empty(); | |
//EMAIL VALIDATION | |
var email = $('input[name="email"]').val(); | |
if (!(/.+/.test(email))){ | |
$('#errors').append('<li>Insert an Email</li>'); | |
} | |
else if (!(/\w+@\w+\.\D{2,3}/.test(email))){ | |
$('#errors').append('<li>Not a valid email</li>'); | |
} | |
//PASSWORD VALIDATION | |
var pass = $('input[name="password"]').val(); | |
if (!(/.+/.test(pass))){ | |
$('#errors').append('<li>Insert a Password</li>'); | |
} | |
else { | |
if (!(/.{8,}/.test(pass))){ | |
$('#errors').append('<li>Password must be at least 8 characters long</li>'); | |
} | |
if (!(/[A-Z]+/.test(pass))){ | |
$('#errors').append('<li>Password must contain a capital letter</li>'); | |
} | |
if (!(/[0-9]+/.test(pass))){ | |
$('#errors').append('<li>Password must contain a number</li>'); | |
} | |
} | |
}); | |
}); |
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