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

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

  2. MrHelpMe

    Tim Slattery Guest

    MrHelpMe <> wrote:

    >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
    >
    >Code:
    >var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;


    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
    MS MVP(DTS)

    http://members.cox.net/slatteryt
     
    Tim Slattery, May 15, 2007
    #2
    1. Advertising

  3. MrHelpMe

    Evertjan. Guest

    Tim Slattery wrote on 15 mei 2007 in
    microsoft.public.inetserver.asp.general:

    >>var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;

    >
    > 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.
    >


    No, that !! is not part of j[ava]script regex
    and those strange leftover )s?

    Try:

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

    or

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


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 16, 2007
    #3
  4. MrHelpMe

    Tim Slattery Guest

    "Evertjan." <> wrote:


    >No, that !! is not part of j[ava]script regex
    >and those strange leftover )s?


    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
    MS MVP(DTS)

    http://members.cox.net/slatteryt
     
    Tim Slattery, May 16, 2007
    #4
  5. MrHelpMe

    MrHelpMe Guest

    On May 16, 9:22 am, Tim Slattery <> wrote:
    > "Evertjan." <> wrote:
    > >No, that !! is not part of j[ava]script regex
    > >and those strange leftover )s?

    >
    > 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
    > MS MVP(DTS)
    > ://members.cox.net/slatteryt


    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.

    /(\d{2})\W(\d{1,2})\W(\d{4})/

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

    Evertjan. Guest

    MrHelpMe wrote on 16 mei 2007 in microsoft.public.inetserver.asp.general:

    > 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.
    >
    > /(\d{2})\W(\d{1,2})\W(\d{4})/


    It is wrong
    1- if you specified the second number to have two characters

    {2} in stead of {1,2}

    try:

    /(\d{2})\W(\d{2})\W(\d{4})/

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

    no need to use ()

    try:

    /\d{2}\W\d{2}\W\d{4}/


    3- if you want only / as a delimiter

    just escape the / by \/

    try:

    /\d{2}\/\d{2}\/\d{4}/

    or:

    /(\d\d\/\){2}\/\d{4}/

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

    try:

    /^(\d\d\/\){2}\/\d{4}$/


    === As you specified jscript in this Asp NG

    >>> Code:
    >>> var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;


    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]:
    <http://tinyurl.com/2hj4zw>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 16, 2007
    #6
  7. MrHelpMe

    Evertjan. Guest

    Tim Slattery wrote on 16 mei 2007 in
    microsoft.public.inetserver.asp.general:

    > "Evertjan." <> wrote:
    >
    >
    >>No, that !! is not part of j[ava]script regex
    >>and those strange leftover )s?

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


    > MrHelpMe wrote on 15 mei 2007 in microsoft.public.inetserver.asp.general:
    > > Code:
    > > var RegExPattern = /(\d{1,2})\W(\d{1,2})\W(\d{4})/;


    The giveaway was the "var" plus that in this ASP NG I would primarily
    expect vbscript or jscript.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 16, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,381
  2. dotnetprogram

    Regular Expression help please...

    dotnetprogram, Dec 17, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    2,076
    Rajesh.V
    Dec 17, 2003
  3. Replies:
    3
    Views:
    352
  4. Replies:
    2
    Views:
    392
    =?Utf-8?B?R3VmZmE=?=
    Apr 21, 2006
  5. KK
    Replies:
    2
    Views:
    706
    Big Brian
    Oct 14, 2003
Loading...

Share This Page