Skip to content

Instantly share code, notes, and snippets.

@driftingwave
Forked from ksolo/form-validator.js
Last active December 26, 2015 02:28
Show Gist options
  • Save driftingwave/7078441 to your computer and use it in GitHub Desktop.
Save driftingwave/7078441 to your computer and use it in GitHub Desktop.
Form Validation
// shorthand for $(document).ready();
$( function() {
$( "button" ).click(function() {
event.preventDefault()
var email = $( "#email" ).val()
var emailRegEx = /.+@.+\..+/;
var OK = emailRegEx.exec( email )
console.log(!OK)
// function validateEmail() {
if (!OK)
$node = '<li>Must be a valid email address</li>'
console.log($node)
$( "#errors" ).append($node)
// }
})
});
<!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 type="password" name="password" />
<button type="submit">Sign Up</button>
<ul id="errors"></ul>
</form>
<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