SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression and date validation

    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

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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>

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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}$/;
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks that helped


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •