【问题标题】:Jquery Validation plugin validate date like 20/August/2013 Safari Bug?Jquery Validation 插件验证日期,例如 20/August/2013 Safari Bug?
【发布时间】:2013-08-25 06:01:19
【问题描述】:

我使用 Jquery Validation 插件并验证事件日期,例如

rules:{
       eventdate: 
       {
       required:true,   
       date: true
       }
     }

日期格式类似于“23/August/2013”​​('dd/MM/yy')

但代码在 Safari 上失败,与 Fx,chrome,opera... 配合使用效果更好...

是 Safari 错误还是 Jquery 验证插件错误?

我知道 Jquery Validation 插件支持自定义验证,如

$.validator.addMethod("customdateValidateRule", function(value, element) 
{  
_isvalidResult=****testfn()***;
return _isvalidResult;
}

我试过Custom date format with jQuery validation plugin 但不会像 dd/mm/yyyy 那样帮助('dd/MM/yy') ,而是正则表达式“23/August/2013" 我不知道!

备注

1) 我使用 Datepicker(JqueryUi) 来选择日期
2) 我正在使用 Jquery Validation 插件(最新)它会验证很多日期格式它也验证“23/August/2013”​​(我假设)但是当我在 Safari 中测试它时它显示无效,在 FX、Chrome、Opera 中显示“有效”..

【问题讨论】:

    标签: jquery regex validation safari jquery-validate


    【解决方案1】:

    问题是因为 Safari 无法正确处理格式。 JQuery 验证插件工具无法验证这种格式(仅在ChromeSafari 中发生)。要解决此问题,请修改验证插件。

    如果你想为 dd/mm/yy 定制它,你可以这样做。

    $.validator.addMethod(
       "customdateValidateRule", 
       function(value, element) {
          return value.match(/^\d{1,2}\/\d{1,2}\/\d{2}$/);
       },
          "Input a date format of dd/mm/yy:"
    );
    

    并将规则验证添加到您的表单中。

    $('#myformname')
       .validate({
           rules : {
            myDate : {
              customdateValidateRule : true
            }
           }
       });
    

    如果你想添加测试方法,你应该可以做类似的事情..

    var isdate = function(value) {
        var isvalid = /^(\d{1,2})\/(\d{1,2})\/(\d{2})?$/;   
        return isvalid.test(value);
    } 
    
    $.validator.addMethod(
       "customdateValidateRule",
       function(value, element) {
          return isdate(value);
       }
    );
    

    如果您需要正则表达式来验证dd/mm/yyyy,请将其更改为..

    /^\d{1,2}\/\d{1,2}\/\d{4}$/
    

    要验证您对 23/August/2013 日期的详细信息,您有几个选项可供选择。

    有捷径:

    /^\d{1,2}\/\b[a-zA-Z]+\/\d{4}$/
    

    正则表达式解释:

    ^              the beginning of the string
     \d{1,2}       digits (0-9) (between 1 and 2 times)
      \/           look for and match '/'
       \b          the boundary between a word char (\w)
        [a-zA-Z]+  any character of: 'a' to 'z', 'A' to 'Z' (1 or more times)
      \/           look for and match '/'
      \d{4}        digits (0-9) (4 times)
    $              before an optional \n, and the end of the string
    

    漫漫长路(具体验证):

    /^\d{1,2}\/\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|
                    May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sept?|September|
                    Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\/\d{4}$/
    

    正则表达式解释:

    ^                     the beginning of the string
     \d{1,2}              digits (0-9) (between 1 and 2 times)
      \/                  look for and match '/'
       \b                 the boundary between a word char (\w)
        (?:               group, but do not capture:
         Jan(?:uary)?|    'Jan', group but don't capture optional 'uary', OR
         Feb(?:ruary)?|   'Feb', group but don't capture optional 'ruary', OR
         Mar(?:ch)?|      'Mar', group but don't capture optional 'ch', OR
         Apr(?:il)?|      'Apr', group but don't capture optional 'il', OR
         May|             'May', OR
         Jun(?:e)?|       'Jun', group but don't capture optional 'e', OR
         Jul(?:y)?|       'Jul', group but don't capture optional 'y', OR
         Aug(?:ust)?|     'Aug', group but don't capture optional 'ust', OR
         Sept?|           'Sep', 't' optional, OR
         September|       'September', OR
         Oct(?:ober)?|    'Oct', group but don't capture optional 'ober', OR
         Nov(?:ember)?|   'Nov', group but don't capture optional 'ember', OR
         Dec(?:ember)?    'Dec', group but don't capture optional 'ember'
        )                 end of grouping
         \/               look for and match '/'
          \d{4}           digits (0-9) (4 times)
    $                     before an optional \n, and the end of the string
    

    【讨论】:

      【解决方案2】:

      正如你所说,如果你使用JqueryUi Datepicker,你有两种方法

      1) 正如@hwnd 所说,您必须编辑验证器插件
      2) 只需在下面添加我的 hack 并满意!

          /*Override the validator's Date Validation with mine! */
          $.validator.methods.date=
          function(value, element) {  
          try{
          var _appleDates=$.datepicker.formatDate("mm/dd/yy",$.datepicker.parseDate("dd/MM/yy",value));
          return (!/Invalid|NaN/.test(new Date(_appleDates).toString()));
          }catch(e){return false;};
          };
      

      然后照你说的使用

      rules:{
             eventdate: 
             {
             required:true,   
             date: true
             }
           }
      

      谢谢你快乐!

      【讨论】:

      • @7-isnotbad 很高兴帮助你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      相关资源
      最近更新 更多