Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Validation plugin validate date like 20/August/2013 Safari Bug?

i use Jquery Validation plugin and validates an event date like

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

date format is like "23/August/2013" ('dd/MM/yy')

but the code fails on Safari , works better with Fx,chrome,opera...

is it a Safari Bug or a Jquery Validation plugin bug ?

i know that Jquery Validation plugin supports custom validation like

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

i tried Custom date format with jQuery validation plugin but wouldn't helped as its in dd/mm/yyyy not for ('dd/MM/yy') ,but regex for "23/August/2013" that i don't know!

Remarks

1) I use Datepicker(JqueryUi) for selecting the date
2) i am using Jquery Validation plugin (latest) it will validate a lot of date formats it also validates "23/August/2013" (i assume) but when i test it in Safari it shows invalid where shown "valid" in FX,Chrome,Opera..

like image 245
internals-in Avatar asked Mar 22 '23 16:03

internals-in


1 Answers

The issue is because Safari does not handle the format properly. JQuery validation plugin tool is not able to validate this format (only happens in Chrome and Safari). To solve this, modify the validation plugin.

If you want it customized for dd/mm/yy, you could do.

$.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:"
);

And add the rule validation to your form.

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

If you want to add the test method instead, you should be able to do something like..

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);
   }
);

If you need the regular expression to validate dd/mm/yyyy, change it to..

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

For validating your specifics of a date for 23/August/2013, you have a couple of options here.

There short way:

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

Regular expression explanation:

^              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

The long way (specific validation):

/^\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}$/

Regular expression explanation:

^                     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
like image 131
hwnd Avatar answered Apr 06 '23 10:04

hwnd