0

i am trying to validate date which Accept only dd/mm/yyyy.

i have use the following code to validate but it always show me alert.

what wrong with my code ??

when i used , var bevalue ='10/10/1989'; it work fine.

And in my code i used, value = $(this).val(); to get value.

then convert it to string

EDIT:

$('input, textarea').live('blur', function () {
        var field = $(this).attr('name');
        var value = $(this).val();

if(field=='birthday')
  {   
        var bevalue = String(value);   
        var regbday =/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;
        if(regbday.test(bevalue) == false)
        {
            alert('please Enter Birth date in dd/mm/yyyy');
           return false;
        }
}

Thanks Everyone for reply my mistake in Regexp. I accepted answer.

3
  • 3
    What's this ? We need more code. And you should first debug to see what's value. Commented Mar 14, 2013 at 11:01
  • 2
    What dystroy is saying is that the code you've posted isn't the cause of the problem. As a side note, you should use ?: to prevent sub-expressions from being captured if you're not using them. Commented Mar 14, 2013 at 11:05
  • 1
    I tested it where value is fake and it works fine. Minus the fact that at the end you are capturing an unlimited amount of numbers +. So therefore I can enter in 10/10/12345679. Removing the + fixes that. if it isn't working, then value is wrong and to help we will need to understand how you are getting value's value like @dystroy said Commented Mar 14, 2013 at 11:12

2 Answers 2

1

It's the regular expression that's not correct,

function checkBday(bevalue) {
    var regbday = /(0[1-9]|[12][0-9]|3[01])[\.\/\-](0[1-9]|1[012])[\.\/\-](19|20)\d\d/;
    if (regbday.test(bevalue) == false) {
        alert(bevalue + ' is not valid\nplease Enter Birth date in dd/mm/yyyy');
        return false;
    }
    return true
}
console.log(checkBday("14.03.2013")) //true
console.log(checkBday("14-03-2013")) //true
console.log(checkBday("14/03/2013")) //true
console.log(checkBday("14.03.12")) //false
console.log(checkBday("12.28.2013")) //false
console.log(checkBday(prompt("Enter Birthday in dd/mm/yyyy")));

This should work, heres an example on JSBIn

Edit
If you only want to match dates which use / as delimiter change the regexp to
/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d/

Sign up to request clarification or add additional context in comments.

3 Comments

I thought he is refereing to the format with dd/mm/yyyy and not to delimiter of the format, as he is matching against . / - in his regexp too
I guess so. The question is worded odd. My bad.
Nevermind =), i updated the answer anyway in case the op really only wants to match /
0

You have the wrong reg-exp, the day and month parts are switched and some characters are not escaped.

Use this formula:

var regbday = /^(0[1-9]|[12][0-9]|3[01])[\- \/\.](0[1-9]|1[012])[\- \/\.](19|20)\d\d+$/;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.