Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a Regular Expression to validate a Date field

Status
Not open for further replies.

megabyte214

Programmer
Nov 7, 2001
40
US
I am trying to validate a form field which needs to be a date in a specific format. I am new to javascript and I need a little help with the javascript synax. The regular expression does work, that I tested. I think it is a problem with how I am using the RegExp constructor. Thanks in advance for any input.

[code}
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
function validate(){

var regex = "^((31(?! (FEB|APR|JUN|SEP|NOV)))|((30|29)(?! FEB))|(29(?= FEB (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])-(JAN|FEB|MAR|MAY|APR|JUL|JUN|AUG|OCT|SEP|NOV|DEC)-((1[6-9]|[2-9]\d)\d{2})$";

var re = new RegExp(regex);

if (document.my_form.v_Date.value.match(re)) {
alert("Valid Date");
}
else
{
alert("Invalid Date");
}
}
</SCRIPT>
</HEAD>
<BODY>
<form METHOD="get" ACTION="" NAME="my_form" onsubmit="return validate()">
<center><TABLE class="form">
<TR>
<TD colspan="2">Date (DD-MON-YYYY):
<INPUT TYPE="text" NAME="v_Date" SIZE="11" >
</TD>
</TR>
<TR>
<TD colspan="2"><center><INPUT TYPE="submit" VALUE=" Submit "></center>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
[/code]
 
Take a look at : thread216-757493 and see if that's what your looking for.
 
Thanks everyone for your ideas. I did figure out what was wrong with my RegExp constructor, the backslashes in the regex variable have to be escaped in order for it to work.
I put in the extra backslashes and it worked fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top