Regular Expression Help please

Discussion in 'ASP General' started by MrHelpMe, May 15, 2007.

  1. MrHelpMe

    MrHelpMe Guest

    I need to create a regular expression for a date field that works only
    in the following format MM/DD/YYYY with the / in the format. No other
    format can be inputted into the field. I need 2 numbers for MM 2
    Numbers for DD and 4 numbers for YYYY. If the users enter 1 number for
    month, 1 for day he should get an alert. Thanks.

    I have this code and thought it was working but it is not. Any help
    would be great. Thanks

    var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;
    MrHelpMe, May 15, 2007
    1. Advertisements

  2. MrHelpMe

    Tim Slattery Guest

    The {1,2} elements tell it to accept one or two of the preceding
    element. You say you want only two digits. I'd do something like this:

    var RegExPattern = !\d{2})/\d{2})/\d{4}!

    Using ! to delimit the RE instead of /, since you have slashes within
    the RE.
    Tim Slattery, May 15, 2007
    1. Advertisements

  3. MrHelpMe

    Evertjan. Guest

    Tim Slattery wrote on 15 mei 2007 in
    No, that !! is not part of j[ava]script regex
    and those strange leftover )s?


    if (/^\d{2}\/\d{2}\/\d{4}$/.test(t)) .....


    if (/^(\d\d\/\){2}\d{4}$/.test(t)) .....
    Evertjan., May 16, 2007
  4. MrHelpMe

    Tim Slattery Guest

    OOP didn't specify what language, this being an ASP group, I assumed
    VBScript. Maybe the trailing semicolon should have meant something.

    I looked at the MSDN documentation for REs, and didn't see anything
    about delimiting them. They use the same example over and over, which
    is a SUB getting an argument and simply using that argument for a
    delimiter. No discussion of enclosing the pattern in quotes or using
    slashes that I could find.

    I did wonder about alternate delimiters. That works in Perl and other
    Unix-like contexts, but I guess not here. IMHO, it would simplify some
    things if it did.

    The leftover parens were a mistake.
    Tim Slattery, May 16, 2007
  5. MrHelpMe

    MrHelpMe Guest

    Hello again everyone and thanks for the replies. Evertjan, I tried
    what you recommended and received errors saying t was undefined.
    However, after looking at this again I did manage to get this to
    work. I expanded on my reg expressions and came up with the
    following. If anyone see's anything wrong with this that I may have
    overlooked please let me know.


    Thanks again.
    MrHelpMe, May 16, 2007
  6. MrHelpMe

    Evertjan. Guest

    MrHelpMe wrote on 16 mei 2007 in microsoft.public.inetserver.asp.general:
    It is wrong
    1- if you specified the second number to have two characters

    {2} in stead of {1,2}



    2- if you specify not to have unneccesaty characters in the regex:

    no need to use ()



    3- if you want only / as a delimiter

    just escape the / by \/





    4- and because now the string 'qwert00/00/0000 asdfg'
    will test true, you will have to insert start ^ and end $ markers:



    === As you specified jscript in this Asp NG

    by using var and ;

    I suggest you use:

    if ( /^(\d\d\/\){2}\/\d{4}$/.test(str) ) doWhatYouWantIfTrue();

    === I suggest you use international valid date strings like

    yyyy-mm-dd or yyyy/mm/dd

    === But if you want only real dates to test true,
    look in the archive of comp.lang.javascript

    for instance here [but all over that NG]:
    Evertjan., May 16, 2007
  7. MrHelpMe

    Evertjan. Guest

    Tim Slattery wrote on 16 mei 2007 in
    The giveaway was the "var" plus that in this ASP NG I would primarily
    expect vbscript or jscript.
    Evertjan., May 16, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.