最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Date Of Birth Validation in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

How can I achieve this?

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.

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.

How can I achieve this?

Share Improve this question edited May 22, 2012 at 5:43 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked May 22, 2012 at 5:40 sauravsaurav 111 gold badge1 silver badge3 bronze badges 9
  • What if JavaScript is disabled in browser? – BalusC Commented May 22, 2012 at 5:42
  • That might be a possibility.But I need it can you please tell me how can i do it in javascript – saurav Commented May 22, 2012 at 5:46
  • I don't know @BalusC, is that still a question in the era of Facebook, Google, etc. with massive jQuery/AJAX-based UIs? – benqus Commented May 22, 2012 at 5:47
  • 1 @benqus: On purpose, just to workaround "the silly validation" :) By the way relatively a lot mobile browsers have it disabled and the mobile market should not be underestimated. – BalusC Commented May 22, 2012 at 5:49
  • 1 @BalusC - So what if JavaScript is disabled? You should always validate server-side, but that doesn't mean it's a bad plan to also do some validation in JS as well... – nnnnnn Commented May 22, 2012 at 6:06
 |  Show 4 more ments

4 Answers 4

Reset to default 2
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. :)

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.

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).

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')}

"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"

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

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.

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:

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

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

发布评论

评论列表(0)

  1. 暂无评论