Search code examples
javascriptjquerymacosmagentoprototypejs

Prototype date format validate error on OSX safari / firefox


I have the following problem on OSX 10.9.4 (Mavericks) Safari and Firefox too:

I use bootstrap datepicker for my date input

<label class="col-sm-3 gray required" for="received_date"><?php echo $dataHelper->__('Received Date') ?> <em>*</em></label>

<div class="col-sm-9 controls input-group date" data-date-format="<?php echo Aion_Ftc_Helper_Data::BOOTSTRAP_DATE_FORMAT ?>"  id="dp_received_date">
    <input type="text" class="required-entry custom-date validate-date" id="received_date" name="received_date">
    <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>

this is an magento site with prototype and prototype validation. The valid date format is "mm-dd-yyyy" and is validating successful on windows chrome, firefox, but is get an error on osx safari and firefox (osx chrome is good too)

validate.js

['validate-date', 'Please enter a valid date (MM-DD-YYYY format).', function(v) {
    if(Validation.get('IsEmpty').test(v)) return true;
    var regex = /^(\d{2})-(\d{2})-(\d{4})$/;

    if(Prototype.Browser.WebKit){
        var d = new Date(v.replace(regex, '$1-$2-$3'));
    } else {
        var d = new Date(v.replace(regex, '$3-$1-$2'));
    }
    //console.log(d);
    return (parseInt(RegExp.$2, 10) == d.getDate()) &&
        (parseInt(RegExp.$1, 10) == (1+d.getMonth()) ) &&
        (parseInt(RegExp.$3, 10) == d.getFullYear());
}],

what's the problem? any tips? I use jquery noconflict, too

thanks!


Solution

  • just simple use this:

    ['validate-date', 'Please enter a valid date (MM-DD-YYYY format).', function(v) {
        return Validation.get('IsEmpty').test(v) || /^(\d{1,2})-(\d{1,2})-(\d{4})$/.test(v);
    
    }],