regular expression and date validation ( 5 Views )
-
hey, i have a question about regular expressions and date validation:
it references thread id # 95177 i think
i have a date field and need to validate the format is mm/yyyy. i am using code from that thread, but it always brings back a false result saying the date format is wrong even if it is not. is the js code good for only netscape. i am using ie 5.0. can anyone solve this simple problem?
<td nowrap><!--- onChange ="validDate();" --->
<input type="text" name="pack_date#get_mdc_SCR_lines.transx_nbr#" value="<cfif #IsDate(pack_date)#>#dateformat(PACK_DATE, "mm/yyyy")#<cfelse>#get_mdc_SCR_lines.pack_date#</cfif>" size="11" maxlength="11" onChange ="return validDate(this.value);">
</td>
<script>
function validDate(theDate) {
//var theDate = #dateformat(PACK_DATE, "mm/yyyy")#;
alert(theDate);
var re_date = new RegExp("^\d{1,2}\/\d{4}$");
alert(re_date);
var isValid = re_date.test(theDate);
alert(isValid);
if (!isValid) { alert("Please enter the date in the correct MM/YYYY format." );
}
return isValid;
}
</script>
thanks
dan
(burcak , Colombia)
Are you sure that the date is being properly formatted by ColdFusion before inserting it in your script? I can't see anything else off the top of my head that would cause that script to fail.
Edit:
Fixed the script you were referring to in that thread. Here's the new version :)
Code:
<script type="text/javascript">
function validDate(theDate) {
var re_date = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$");
var isValid = re_date.test(theDate);
if (!isValid) {
alert("Please enter the date in the correct DD/MM/YYYY format.");
}
return isValid;
}
</script>
(dilek, Japan)
When creating patterns as strings (ie using the RegExp constructor) you need to escape your backslashes -- because in a string context they are there to escape the proceeding character, and aren't really in the string at all.
var re_date = new RegExp("^\\d{1,2}\\/\\d{4}$" );
That should do it. Or, you can just use a pattern literal
var re_date = /^\d{1,2}\/\d{4}$/;
(mustafa , Zambia)
thanks that helped
(gülseren, Italy)
Related Topics ... (or search in 1.720.883 topics !)
help on simple regular expression for date validation (4) pcre regular expression validation (2) email validation through regular expression (2) string validation via regular expression ? (3) ignoring regular expression validation (1) dhtml form validation - regular expression (3) phone number regular expression validation (9) help needed with my url validation regular expression (6) regular expression -date (2)
copyright © 2007-2031 Pfodere.COM ( 5 Pfoyihuee Online )
|