JavaScript 中的出生日期验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10696283/
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 02:33:28  来源:igfitidea点击:

Date Of Birth Validation in JavaScript?

javascript

提问by saurav

I have a date of birth like 12-08-1989 in text box in HTML.I want to validate that the user must be of 18 years old in javascript.

我在 HTML 的文本框中有一个像 12-08-1989 这样的出生日期。我想验证用户在 javascript 中必须年满 18 岁。

I have used Date function in javascript but it seems like it accept YYYY-MM-DD format but i want to validate in DD-MM-YYYY.

我在 javascript 中使用了 Date 函数,但它似乎接受 YYYY-MM-DD 格式,但我想在 DD-MM-YYYY 中进行验证。

How can I achieve this?

我怎样才能做到这一点?

回答by verisimilitude

var pattern =/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/;
if(pattern.test(str_input_date))
{
     alert("valid date");
}

This should give you a start. :)

这应该给你一个开始。:)

回答by AndreyM

You should probably be using some validation framework, like jQuery.validation. This is by far more consistent way to handle validation in your code. If this is the only validated field in your app, you can, of course, use naive implementation as provided above.

您可能应该使用一些验证框架,例如jQuery.validation。这是在代码中处理验证的更加一致的方式。如果这是您的应用程序中唯一经过验证的字段,您当然可以使用上面提供的简单实现。

Whether you need some advanced date validation rules, you could use a JS date framework, like Moment.js(or Date.js, which is pretty outdated at the moment).

无论您是否需要一些高级日期验证规则,您都可以使用 JS 日期框架,例如Moment.js(或Date.js,目前已经过时)。

回答by nnnnnn

"I have used Date function in javascript but it seems like it accept YYYY-MM-DD format but i want to validate in DD-MM-YYYY"

“我在 javascript 中使用过 Date 函数,但它似乎接受 YYYY-MM-DD 格式,但我想在 DD-MM-YYYY 中进行验证”

You can create a Date object by passing the year, month and day (and, optionally, the hour, minute, second, and millisecond) values separately:

您可以通过分别传递年、月和日(以及可选的小时、分钟、秒和毫秒)值来创建 Date 对象:

var aDate = new Date(2012, 5, 22);

So if you use a regex or other string methods to extract the pieces from what the user entered then you can create a date from DD-MM-YYYY (or whatever other) format you like.

因此,如果您使用正则表达式或其他字符串方法从用户输入的内容中提取片段,那么您可以从 DD-MM-YYYY(或任何其他您喜欢的)格式创建日期。

The way I'd test if the user is at least 18 years old would be to add 18 years to their date of birth and see if that is on or before today's date:

我测试用户是否至少 18 岁的方法是在他们的出生日期上加上 18 岁,看看这是否在今天或之前:

aDate.setFullYear( aDate.getFullYear() + 18 )

The various methods available on a Date object are listed at MDN.

在 MDN列出了 Date 对象上可用的各种方法。

回答by Hilmi

you can use the date class, just like this

你可以使用日期类,就像这样

try{
    var x=new Date('1/1/2001');
    var Cnow=new Date();//current Date
    if(Cnow.getFullYear()- x.getFullYear()<18){
    alert('not old enough');
    }
    else{
      //success !!!
    }

}
catch(ejs){alert('invalid')}