Skip to content

Instantly share code, notes, and snippets.

@nbatalla03
Forked from ksolo/form-validator.js
Last active December 24, 2015 01:09
Show Gist options
  • Save nbatalla03/6721211 to your computer and use it in GitHub Desktop.
Save nbatalla03/6721211 to your computer and use it in GitHub Desktop.
Form Validation
// shorthand for $(document).ready();
$(function(){
//Your code...
$('form').submit(function(event){
var email = $("#email").val();
var password = $('#password').val();
if(email == '') {
$('#errors').html("<li>You need an e-mail address... Everyone has one!</li>");
event.preventDefault();
}
if(password == '') {
$('#errors').html("<li>Why you no password? You a locksmith or sumthin?</li>");
event.preventDefault();
}
})
});
<!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 id="email" type="text" name="email" />
<label for="password">Password</label>
<input id="password" 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