Skip to content

Instantly share code, notes, and snippets.

@SriniBlog
Created December 25, 2015 15:32
Show Gist options
  • Save SriniBlog/232c9dbe251c26bbc594 to your computer and use it in GitHub Desktop.
Save SriniBlog/232c9dbe251c26bbc594 to your computer and use it in GitHub Desktop.
This is a code snippet to show how to validate form using JQuery
/*
HTML Form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-1.11.2.min.js"></script>
<!-- CUSTOM JS -->
<script src="js/tester.js" type="text/javascript"></script>
<script src="js/jquery.cookie.js"></script>
</head>
<body>
<h1> Testing Form Validation </h1>
<form method="POST" id="frmTest">
<input type="text" id="testfield">
<button type="submit" id="btnSubmit">Send Message</button>
<button type="reset" id="btnReset">Reset</button>
</form>
</body>
</html>
*/
// tester.js
$(document).ready(function () {
//Stops the submit request
$("#frmTest").submit(function(e){
e.preventDefault();
});
$("#btnReset").click(function(e){
resetForm("#frmTest");
});
//checks for the button click event for New
$("#btnSubmit").click(function(e){
testField = $("#frmTest #testfield");
if( testField.val() == null || testField.val() == "" ){
alert('Test Field cannot be empty!');
testField.focus();
return false;
}
testFieldValue = testField.val();
//Validation is fine
}
} //End of JQuery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment