Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom date format with jQuery validation plugin

How can I specify a custom date formate to be validated with the Validation Plugin for jQuery?

like image 976
Bruno Avatar asked Feb 04 '09 13:02

Bruno


People also ask

How to validate date format in JQuery?

Below jQuery code shows exactly the same thing. $(function() { $('#btnSubmit'). bind('click', function(){ var txtVal = $('#txtDate'). val(); if(isDate(txtVal)) alert('Valid Date'); else alert('Invalid Date'); }); });

How do you check if the date is in dd mm yyyy format in Javascript?

The toISOString() method returns a string formatted as YYYY-MM-DDTHH:mm:ss. sssZ . If the ISO representation of the Date object starts with the provided string, then the date string is valid and formatted as DD/MM/YYYY .

What is JQuery validation?

Validation in JQuery: Using JQuery, a form is validated on the client-side before it is submitted to the server, hence saves the time and reduce the load on the server. Form Validation means to validate or check whether all the values are filled correctly or not.


1 Answers

You can create your own custom validation method using the addMethod function. Say you wanted to validate "dd/mm/yyyy":

$.validator.addMethod(     "australianDate",     function(value, element) {         // put your own logic here, this is just a (crappy) example         return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);     },     "Please enter a date in the format dd/mm/yyyy." ); 

And then on your form add:

$('#myForm')     .validate({         rules : {             myDate : {                 australianDate : true             }         }     }) ; 
like image 86
nickf Avatar answered Sep 16 '22 14:09

nickf