0

I've implemented a new function to validate a single date field with format 'dd/mm/YYYY'. It works well but if another field is wrong, it's error message appears.

Here is my js

var frenchDateReg = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g;    
Validation.add('validate-date-fr', 'Please enter a correct date. For example 24/12/2017', function(v) {
        console.log(v);
        console.log(Validation.get('IsEmpty').test(v));
        console.log(frenchDateReg.test(v));
        console.log(frenchDateReg);
        return Validation.get('IsEmpty').test(v) || frenchDateReg.test(v)
    });

on log when error appears and not I have the same showing :

[Log] 05/05/1995 (fonctions.js, line 8)
[Log] false (fonctions.js, line 9)
[Log] true (fonctions.js, line 10)
[Log] /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g (fonctions.js, line 11)

But in front I get the error : enter image description here Theses fields are in the customer edit form. So to generate the date field I override the template customer/account/widget/dob.phtml like this :

<label for="dob"<?php if ($this->isRequired()) echo ' class="required"' ?>><?php if ($this->isRequired()) echo '<em>*</em>' ?><?php echo $this->__('Date of birth') ?></label>
<div class="input-box customer-dob">
    <?php
    $time = $this->getTime();
    $date = null;
    if ($time) {
        $date = $this->getDay().'/'.$this->getMonth().'/'.$this->getYear();
    }
    ?>
    <input type="text" id="dob" name="dob" value="<?php echo $date ?>" title="dob" class="input-text required-entry validate-date-fr" <?php echo $this->getFieldParams() ?> placeholder="<?php echo $this->__('Fill this field') ?>" />
</div>
<script type="text/javascript">
    //<![CDATA[
    var customer_dob = new Varien.DOB('.customer-dob', <?php echo $this->isRequired() ? 'true' : 'false' ?>, '<?php echo $this->getDateFormat() ?>');
    //]]>
</script>

Can you help me please ? I don't understand why the error is showing with a correct js validation return.

0

IDK why this bug but with this js, the error not appears again :

Validation.add('validate-date-fr', 'Please enter a correct date. For example 24/12/2017', function(v) {
        return Validation.get('IsEmpty').test(v) || /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g.test(v)
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.