2012-01-09 150 views
2

我正在使用jQuery驗證插件來驗證我的表單,我如何驗證此日期格式的自定義日期DD-MMM-YYY(23-Mar-2012)。jQuery驗證插件:驗證自定義日期格式

+0

使用jQuery驗證插件[自定義日期格式可能重複在(http://stackoverflow.com/questions/511439/custom-date-format-with-jquery-validation-plugin) – 2013-11-24 06:41:43

回答

5

Create a custom validator

jQuery.validator.addMethod("mydate", function(value, element) { 
    return this.optional(element) || /^\d\d?-\w\w\w-\d\d\d\d/.test(value); 
}, "Please specify the date in DD-MMM-YYYY format"); 
3

http://www.intelligrape.com/blog/2010/03/31/client-side-date-validation-using-jquery-plugins/

jQuery.validator.addMethod("customDateValidator", function(value, element) { 
     // parseDate throws exception if the value is invalid 
     try{jQuery.datepicker.parseDate('m/dd/yy', value);return true;} 
     catch(e){return false;} 
    }, 
    "Please enter a valid date" 
); 

檢查還引用後:Custom date format with jQuery validation plugin

另外:Validate two dates of this "dd-MMM-yyyy" format in javascript