正则表达式,用于在jquery中以dd / mm / yy格式验证日期

时间:2013-08-27 06:29:49

标签: jquery validation date

我正在使用此代码使用jquery验证我的文本字段,我已经验证了我的电子邮件名称和联系人但不能做dob即出生日期。是否有任何正则表达式以dd / mm / yy格式验证日期。

$(function(){

var email = $('.email'),
    name = $('.name'),
    contact = $('.contact'),
    dob = $('.dob');



email.on('blur',function(){

    // Very simple validation
    if (!/^\S+@\S+\.\S+$/.test(email.val())){
        email.parent().addClass('error').removeClass('success');
    }
    else{
        email.parent().removeClass('error').addClass('success');
    }

});

name.on('blur',function(){

    // Very simple validation
    if (!/^\[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(name.val())){
                name.parent().removeClass('error').addClass('success');         
    }
    else{
        name.parent().addClass('error').removeClass('success');
    }

});

contact.on('blur',function(){

    // Very simple validation
    if (!/^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(contact.val())){
        contact.parent().addClass('error').removeClass('success');
    }
    else{
        contact.parent().removeClass('error').addClass('success');
    }

});

dob.on('blur',function(){

    // Very simple validation
    if (!/^\d\d?-\d\d-\d\d.test(dob.val())){
        dob.parent().removeClass('error').addClass('success');
    }
    else{           
        dob.parent().addClass('error').removeClass('success');
    }

       });


    });

当我使用下面的代码时,它正确检查mm / dd / yyyy但是我想用dd / mm / yyyy

  dob.on('blur',function(){

    // Very simple validation
    if (!/^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/.test(dob.val())){
        dob.parent().addClass('error').removeClass('success');

    }
    else{           
        dob.parent().removeClass('error').addClass('success');
    }

});

3 个答案:

答案 0 :(得分:2)

这是针对dd / mm / yyyy

^\s*((31([-/ ])((0?[13578])|(1[02]))\3(\d\d)?\d\d)|((([012]?[1-9])|([123]0))([-/ ])((0?[13-9])|(1[0-2]))\12(\d\d)?\d\d)|(((2[0-8])|(1[0-9])|(0?[1-9]))([-/ ])0?2\22(\d\d)?\d\d)|(29([-/ ])0?2\25(((\d\d)?(([2468][048])|([13579][26])|(0[48])))|((([02468][048])|([13579][26]))00))))\s*$

Matches 
29-2-2000 | 31/12/9999 | 1 1 0000 | 29/02/0000 | 8/3/9841
Non-Matches 
29/2/1900 | 29/02/2001 | 31/04/2000 | 28/02-1934 | 1/13/1978

从这里采取

http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5&AspxAutoDetectCookieSupport=1

答案 1 :(得分:1)

这是我的代码,现在工作正常。任何人都可以使用它

 $(function(){

var email = $('.email'),
    name = $('.name'),
    contact = $('.contact'),
    dob = $('.dob'),
    doj = $('.doj');



email.on('blur',function(){

    // Very simple validation
    if (!/^\S+@\S+\.\S+$/.test(email.val())){
        email.parent().addClass('error').removeClass('success');
    }
    else{
        email.parent().removeClass('error').addClass('success');
    }

});

name.on('blur',function(){

    // Very simple validation
    if (!/^[a-zA-Z\s]+$/.test(name.val())){
        name.parent().addClass('error').removeClass('success');         
    }
    else{
        name.parent().removeClass('error').addClass('success');
    }

});

contact.on('blur',function(){

    // Very simple validation
    if (!/^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?[0,10]$/.test(contact.val())){
        contact.parent().addClass('error').removeClass('success');
    }
    else{
        contact.parent().removeClass('error').addClass('success');
    }

});

dob.on('blur',function(){

    // Very simple validation
    if (!/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[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))))$/.test(dob.val())){
        dob.parent().addClass('error').removeClass('success');

    }
    else{           
        dob.parent().removeClass('error').addClass('success');
    }

});

doj.on('blur',function(){

    // Very simple validation
    if (!/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[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))))$/.test(doj.val())){
        doj.parent().addClass('error').removeClass('success');

    }
    else{           
        doj.parent().removeClass('error').addClass('success');
    }

    });


   });

the code for html file is


    <form>
   <div class="group_field">
 <div class="text">E-mail :</div>
 <div class="field"><input name="" type="text" class="email text_field" /></div>
 <div class="validation"></div>
 </div>

<div class="group_field">
<div class="text">Contact :</div>
<div class="field"><input name="" type="text" class="contact text_field"  /></div>
<div class="validation"></div>
</div>

<div class="group_field">
<div class="text">Doj :</div>
<div class="field"><input name="" type="text" class="doj text_field" /></div>
<div class="validation"><!--<img src="images/tick.png"  />--></div>
</div>

<div class="group_field">
<div class="text">dob :</div>
<div class="field"><input name="" type="text" class="dob text_field" /></div>
<div class="validation"><!--<img src="images/tick.png"  />--></div>
</div>

<div class="group_field">
<div class="text">name  :</div>
<div class="field"><input name="" type="text" class="name text_field" /></div>
<div class="validation"><!--<img src="images/tick.png"  />--></div>
</div>
 </form>

答案 2 :(得分:0)

试试这个

dob.on('blur',function(){
// regular expression to match required date format 
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

if (dob.value != '' && !dob.value.match(re)){
    alert("Invalid date format: " + dob.value);
    dob.parent().addClass('error').removeClass('success');
}
else{           
    dob.parent().removeClass('error').addClass('success');
}
});

这是一个很好的实现示例 http://www.the-art-of-web.com/javascript/validate-date/#.UhxM8D_hN8w