开发者

Regular expression date control with jQuery

开发者 https://www.devze.com 2023-03-01 02:03 出处:网络
How to check date format with regular expression in input text? 开发者_开发知识库Format: 20.04.1986I\'d use Datejs or some other validation library as they\'ve been tested and used for awhile, but if

How to check date format with regular expression in input text?

开发者_开发知识库Format: 20.04.1986


I'd use Datejs or some other validation library as they've been tested and used for awhile, but if you just want some raw regex, here's what I came up with quickly. This regex will work, but it won't check that the year is over a certain reasonable date; it will only check that it's four numbers long.

if(date.search(/([1-9]\d)\.((0[1-9])|(1[0-2]))\.\d{4}/) > -1){
    //do stuff when date is valid
}else{
    //do stuff when date is not valid
}


Try:

function call()
{    
    if ($("#date").val().search((/^\d{2}[.]\d{2}[.]\d{4}$/))>-1) {
        $("#date").css('background-color', '#FF0000'); 
    }
    else {
        alert("good");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号