Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Regex help

  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts

    Regex help

    Hi.

    I want to make a simple regex, that checks if a user has supplied their Date of Birth correctly (YYYY-MM-DD)

    Right now I have:

    Code:
    if(!preg_match("/^([0-9]+)-([0-9]+)-([0-9]+)$/i", $dob)){
    	echo "You have entered an invalid Date of Birth.";
    }
    I may be wrong on that regex, but it works at this point. But how do I set the limit on 4 numbers only within the first section, 2 numbers only on the second section and 2 numbers only on the third? Because right now I can simply enter 1999999-54214-35435 and it would return true.
    Last edited by [vengeance]; 01-17-2010 at 04:47 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    you can enter the amout of numbers in curly brackets:
    Code:
    // months don’t exceed 12 and days don’t exceed 31
    @^\d{4}-[01]\d-[0-3]\d$@
    EDIT: the "i" parameter is not necessary, numbers only have one case
    Last edited by Dormilich; 01-17-2010 at 04:05 PM.

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Dormilich View Post
    you can enter the amout of numbers in curly brackets:
    Code:
    // months donít exceed 12 and days donít exceed 31
    @^\d{4}-[01]\d-[0-3]\d$@
    EDIT: the "i" parameter is not necessary, numbers only have one case
    Thanks, I wasn't sure what the "i" parameter meant, I just remember that I used it previously on other regexes. However, I do not understand your regex code. Could you explain to me, what it exactly does?

    What does the "d" and "@" mean? And what do the "[01]" and "[0-3]" mean?

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    \d - a digit

    \d{4} - four digits

    @ - the used RegEx string delimiter. PHP allows any character you want, so it doesnt matter if it’s /, #, @, % or whatever you like. additionally, if you don’t use / you don’t need to escape it in the RegEx

    [01] - one out of 0 or 1

    [0-3] - one out of the range of 0 to 3

  • Users who have thanked Dormilich for this post:

    [vengeance] (01-17-2010)

  • #5
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Dormilich View Post
    \d - a digit

    \d{4} - four digits

    @ - the used RegEx string delimiter. PHP allows any character you want, so it doesnt matter if itís /, #, @, % or whatever you like. additionally, if you donít use / you donít need to escape it in the RegEx

    [01] - one out of 0 or 1

    [0-3] - one out of the range of 0 to 3
    Thanks.

    That's fine, I should be able to work on it by myself from now on.


  •  

    Posting Permissions

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