Having problem with date format in Javascript validations [Resolved]

Posted by Rasagna under JavaScript on 12/22/2015 | Points: 10 | Views : 398 | Status : [Member] | Replies : 4
Hii,
Facing problem with JavaScript Date format validation.Need to validate Date in Textbox with Id as DOB with MM/DD/YYYY format i have used the below code.
 if (document.getElementById("DOB").value == "")
{
alert("Please Enter Date Of Birth");
DOB.focus();
}
else if (!document.getElementById("DOB").value.match(/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/))
{
alert("Invalid date Format");
DOB.focus();
}

can any one tell me where the fault is?




Responses

Posted by: Rajayadav on: 12/22/2015 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi, Try like like


if (document.getElementById("DOB").value == "")
{
alert("Please Enter Date Of Birth");
DOB.focus();
}
else if (isValidDate(document.getElementById("DOB").value))
{
alert("Invalid date Format");
DOB.focus();
}


function isValidDate(subject){
if (subject.match(/^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/)){
return true;
}else{
return false;
}
}



Thanks
-Raja

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Try

var dt="01/30/2015";   // or  2/3/2015 

var dt_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
if (!(dt_regex.test(dt))) alert("Invalid date Format");
else alert("Valid Date");


--
Thanks & Regards,
RNA Team

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasagna on: 12/22/2015 [Member] Starter | Points: 25

Up
0
Down
@Rajayadav thanks for reply but it works only if we have 2 digits as day or month.when i use calendar control some times i get date as 2/3/2015 then it is not working.


Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasagna on: 12/22/2015 [Member] Starter | Points: 25

Up
0
Down
@Rajnilari2015 its not working..can you please elaborate

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response