-
-
Save alexcodreanu86/9256894 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
// shorthand for $(document).ready(); | |
$(function(){ | |
//Your code... | |
$("#submit").attr('disabled','disabled'); | |
var validateEmail = function(email){ | |
return email.match(/\w+@\w+\.\w+/) != null; | |
}; | |
var validatePassword = function(password){ | |
return passwordHasDigit(password) && passwordHasLength(password) && hasCapitalLetter(password) | |
}; | |
var passwordHasDigit = function(password){ | |
return password.match(/\d/) !== null | |
}; | |
var passwordHasLength = function(password){ | |
return password.length >= 8 | |
}; | |
var hasCapitalLetter = function(password){ | |
return password.match(/[A-Z]/) !== null | |
}; | |
$(document).on('keyup', function(){ | |
var passData = $('#password').val(); | |
var emailData = $('#email').val(); | |
if (validateEmail(emailData)){ | |
$('.email_error').addClass('green') | |
} else { | |
$('.email_error').removeClass('green') | |
} | |
if (passwordHasDigit(passData)){ | |
$('#digit').addClass('green'); | |
} else { | |
$('#digit').removeClass('green') | |
} | |
if (passwordHasLength(passData)){ | |
$('#length').addClass('green'); | |
} else { | |
$('#length').removeClass('green'); | |
} | |
if (hasCapitalLetter(passData)){ | |
$('#capital').addClass('green'); | |
} else { | |
$('#capital').removeClass('green') | |
} | |
if (validatePassword(passData) && validateEmail(emailData)) { | |
console.log(validatePassword(passData) && validateEmail(emailData)) | |
$("#submit").removeAttr("disabled"); | |
} else{ | |
console.log("boooooooo"); | |
$("#submit").attr('disabled','disabled'); | |
} | |
}); | |
}); |
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 id="email"type="text" name="email" /> | |
<label for="password">Password</label> | |
<input id="password"type="password" name="password" /> | |
<button id="submit" type="submit">Sign Up</button> | |
<ul id="errors"> | |
<li class="email_error">Must be valid email address</li> | |
<li id="length" >Password must have at least 8 characters</li> | |
<li id="capital">Password must have at least one capital letter</li> | |
<li id="digit">Password must have at least one numeric character</li> | |
</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> |
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; | |
} | |
.green{ | |
color: #5CF520; | |
} | |
.red { | |
color:red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment