Last active
September 23, 2018 11:06
-
-
Save santosh/fc6df7ef233d36a3c8e20ac79ba71457 to your computer and use it in GitHub Desktop.
Basic form validation #JavaScript
This file contains hidden or 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" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Form Validation Example</title> | |
</head> | |
<body> | |
<form action="/register" id="registration" method="get" accept-charset="utf-8"> | |
<input type="text" name="email" placeholder="Email" autofocus autocomplete="off" /> | |
<input type="password" name="password" placeholder="Password" /> | |
<input type="password" name="confirmation" placeholder="Password (again)" /> | |
<label><input type="checkbox" name="agreement"/> I agree </label> | |
<input type="submit" value="Submit" /> | |
</form> | |
<script charset="utf-8" src="validation.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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
let form = document.getElementById("registration"); | |
form.onsubmit = function() { | |
if (!form.email.value) { | |
alert("So.. you don't have any email?"); | |
return false; | |
} else if (!form.password.value) { | |
alert("Really? No password?"); | |
return false; | |
} else if (form.password.value != form.confirmation.value) { | |
alert("Passwords don't match"); | |
return false; | |
} else if (!form.agreement.checked) { | |
alert("You can't proceed with agreeing to the TOS."); | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment