Anda di halaman 1dari 1
jQuery Validation There are many situations when you need to validate entered data in HTML forms with jQuery. We created several examples that are most used among web developers so please find it below. If you have any other suggestion that we could write about please contact us as let us know. For the beginning here are the main HTML and jQuery code: ="testfield” type="text" /> | // Nalidate User Id a if(validateUserName(data)) { : alert( "Entered data is an valid email address.*); Ft } /} Nalidate Date if(validDate(data)) { alert('Entered data is an valid email address."); - ; /f Nalidate email address if(validemailAddress(data)) { alert(‘Entered data is an valid email address."); } pi ys Phone Numbers Validation The following function could be used for phone number validation: a * Function validates phone number a} */ function validatePhoneNumber(data) { var pattern=new Regexp(/*[0-9-+]+$/)3 return pattern.test (data) ; } User Name Validation ‘You can need use this method to validate user names or id allowing only alpha-numerical characters and underscores pe * Function validates user name. Allowed characters are alpha-numerals and unde a3 function validateUserName(data) { var pattern= new Regexp(/*[a-20-9_]+8/); return pattern.test (data); «l é » Date Validation The following method checks whether passed value is a valid date ye * Function validates date al + function validateDate(data) { var pattern= new Regexp(/\b\d{1,2}[\/-]\a{1,2}[\/- Je {4})\b/) return pattern.test (data) ; } Email Address Validations Validating email address with jQuery is one of the most needed function ye * Function validates email " function validatetmail(data) { var pattern= new RegExp(/*[\w-]+(\.[\w-]#) *@¢[\w-] #4. )#[2-24-Z]4$/) 5 return pattern.test (data) ;