asp.net 中 mm/dd/yyyy 格式的 Javascript 日期验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9062400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:32:43  来源:igfitidea点击:

Javascript Date validation for mm/dd/yyyy format in asp.net

javascriptjqueryasp.netdate

提问by Vishal Avhad

I want to validate BirthDate, which should be in "mm/dd/yyyy" format, on the client side.

我想在客户端验证出生日期,它应该是“mm/dd/yyyy”格式。

I have tried it as following, but it is not working properly:

我已经尝试过如下,但它不能正常工作:

$("#btnUpdateEditCB3").click(function(event) {
    var txtBirthDate = $('#<%= txtBirthDateCB3.ClientID %>').val();
    var txtNickName = $('#<%= txtNickNameCB3.ClientID %>').val();
    if (txtBirthDate != "") {
        if (txtBirthDate.match(/^(?:(0[1-9]1[012])[\/.](0[1-9][12][0-9]3[01])[\/.](1920)[0-9]{2})$/)) {
            alert("Please enter date in mm/dd/yyyy format");
            $('#<%= txtBirthDateCB3.ClientID %>').focus();
            return false;
        }
    }
});

回答by devson

Below links explains the same...see if it helps you.

下面的链接解释了相同的...看看它是否对你有帮助。

Validate date using jquery

使用 jquery 验证日期

Validate date format using jquery

使用 jquery 验证日期格式

回答by Salman A

I recommend that you use the JavaScript Date() object along with regular expressions to validate a date. You can use a variant of this codeas follows:

我建议您使用 JavaScript Date() 对象和正则表达式来验证日期。您可以使用此代码的变体,如下所示:

function ValidateCustomDate(d) {
    var match = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(d);
    if (!match) {
        // pattern matching failed hence the date is syntactically incorrect
        return false;
    }
    var month = parseInt(match[1], 10) - 1; // months are 0-11, not 1-12
    var day   = parseInt(match[2], 10);
    var year  = parseInt(match[3], 10);
    var date  = new Date(year, month, day);
    // now, Date() will happily accept invalid values and convert them to valid ones
    // therefore you should compare input month/day/year with generated month/day/year
    return date.getDate() == day && date.getMonth() == month && date.getFullYear() == year;
}
console.log(ValidateCustomDate("1/01/2011"));  // false
console.log(ValidateCustomDate("01/1/2011"));  // false
console.log(ValidateCustomDate("01/01/2011")); // true
console.log(ValidateCustomDate("02/29/2011")); // false
console.log(ValidateCustomDate("02/29/2012")); // true
console.log(ValidateCustomDate("03/31/2011")); // true
console.log(ValidateCustomDate("04/31/2011")); // false

回答by Muthu

Is there any specific reason for not using datepicker formatting? Suggest using a jquery datepicker where you can set the formats.

不使用日期选择器格式有什么具体原因吗?建议使用 jquery 日期选择器,您可以在其中设置格式。

jquery date picker date time formatting

jquery 日期选择器日期时间格式

回答by Deepak Kothari

If you want to validate date in mm/dd/yyyy format using javascript you can use the following snippet of code.

如果您想使用 javascript 以 mm/dd/yyyy 格式验证日期,您可以使用以下代码片段。

txtdate is having textbox id

txtdate 有文本框 ID

Consider the below code.

考虑下面的代码。

    function isValidDate(txtdate) {
    var txtDate = "#" + txtdate;
    var dateString = $(txtDate).val();
    var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
    if (!(date_regex.test(dateString))) {
    alert("Date Must Be in mm/dd/yyyy format");
}}