...

View Full Version : Resolved Regex help



[vengeance]
01-17-2010, 04:29 PM
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:



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.

Dormilich
01-17-2010, 04:52 PM
you can enter the amout of numbers in curly brackets:

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

[vengeance]
01-17-2010, 05:30 PM
you can enter the amout of numbers in curly brackets:

// 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?

Dormilich
01-17-2010, 05:34 PM
\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

[vengeance]
01-17-2010, 05:46 PM
\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. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum