Created
June 14, 2012 14:09
-
-
Save davidicus/2930558 to your computer and use it in GitHub Desktop.
A collection of jQuery form validations and input formatting.
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
// Form Validation | |
jQuery('document').ready(function() { | |
/**************************** | |
phone number restriction | |
*****************************/ | |
//restrict input to numeric values, ., and - for phone numbers | |
jQuery('.numbersOnly').keyup(function () { | |
this.value = this.value.replace(/[^0-9\.\-]/g,''); | |
}); | |
//restrict the amount of digits user can eter for a phone number or zip | |
//firt set your variable to the value of your element | |
var phone = jQuery('#phone').val(); | |
/*then make if statement saying if value of variable is less or equal than 9 | |
or greater or equal to 11 run the alert and stop submission.*/ | |
if (phone.length <= 9 || phone.length >= 13 ) | |
{ | |
alert("You must enter a valid phone number"); | |
return false; | |
} | |
/************************************* | |
onClick trigger for validation | |
*************************************/ | |
//call your element that you want to trigger with click event handler | |
jQuery('#yourButtonInput').click(function(){ | |
//set a variable for a form iput to its value | |
var zip = jQuery('#zippy').val(); | |
/*set up if statement asking if your variable | |
above is equal to null or an empty string or any default string*/ | |
if (zip === null || zip === "" || zip === "Zip Code") | |
//if above if statement is true set off alert and stop submission | |
{ | |
alert("Please enter your zip code."); | |
return false; | |
} | |
//else say thanks and cary on either by alert or displaying a hidden div | |
alert("Thanks for your submission"); | |
}); | |
/******************************** | |
validation for check boxes | |
*********************************/ | |
/*if statement asking if your checkbox element (.checkBoxClass)which | |
is checked(:checked)is equal to undefined. Meaning that it is not checked. | |
If that is true run alert and stop submission*/ | |
if (jQuery('.checkBoxClass:checked').val() == undefined) { | |
alert("You must check the box stating you have read and agree with the terms & conditions."); | |
return false; | |
} | |
/*********************************** | |
validate email address | |
***********************************/ | |
//set variable for email input to its value | |
var emailVal = jQuery('.emailInputClass').val(); | |
//set a regular expression dictating the characters allowed | |
var patternEmail = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; | |
//if statement saying if email variable tested against the pattern variable is not true set off alert and stop submission | |
if (!patternEmail.test(emailVal)) { | |
alert("Invalid email"); | |
return false; | |
} | |
/****************************************** | |
validate date format of mm/dd/yyyy | |
******************************************/ | |
//set variable for date input to its value | |
var dateVal = jQuery('.dateInputClass').val(); | |
//set a regular expression dictating the characters allowed and format allowed | |
var pattern = /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/; | |
//if statement saying if date variable tested against the pattern variable is not true set off alert and stop submission | |
if (!pattern.test(dateVal)) { | |
alert("Invalid date! Please Input in (dd/mm/yyyy) format!"); | |
return false; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment