Results 1 to 5 of 5

Thread: [RESOLVED] Javascript/RegEx : \d{2,4}

  1. #1

    Thread Starter
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    [RESOLVED] Javascript/RegEx : \d{2,4}

    I'm using this code to validate a date
    Code:
    if ( testStr.match(/^\d{1,2}\/\d{1,2}\/\d{2,4}$/) == null )
    {
    	sMessage += 'Date is not in format DD/MM/YYYY';
    	document.getElementById('startdate').focus();
    }
    Now, I have a limited understanding of Regular Expressions but doesn't \d{2,4} mean that it matches either 2 or 4 numeric characters? Because it seems to be accepting 3 numeric characters too.
    Last edited by TomGibbons; Mar 3rd, 2004 at 12:05 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You're right, but I'm not sure why it's not working. So as a last resort, use

    d{4} instead of d{2,4}

  3. #3

    Thread Starter
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345
    Okay. Thanks anyway It could just be a FireFox issue.

  4. #4
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    \d{2,4} means atleast 2 digits, no more than 4. You could try \d{2}|\d{4}. That should give you what you want. You may need to wrap it in a parenthesis.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  5. #5

    Thread Starter
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345
    Ahhh I see. Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width