Jump to content

Preg_Match...


phpSensei

Recommended Posts

I really Suck with delimeters and Regular expressions so can someone help me?

 

I have a date of birth field which needs to be in this format dd/mm/yyyy...

 

But I want the script to detect if the format is as followed "

 

Number Number / Number Number / Number Number Number Number/

 

So it contains no Strings, and I want to use some kind of explode to see if it has three sections and each seperated by "/"...

 

Anyone help?

Link to comment
https://forums.phpfreaks.com/topic/63425-preg_match/
Share on other sites

A very basic regex pattern:

$dob = '02/01/1964';

echo '<b>DOB: </b>' . $dob . '<br />';


if(preg_match('|[0-9]{2}/[0-9]{2}/[0-9]{4}|', $dob))
{
    echo 'Date of birth is in the correct format';
}
else
{
    echo 'Date of birth is not in the correct format';
}

Link to comment
https://forums.phpfreaks.com/topic/63425-preg_match/#findComment-316314
Share on other sites

If you don't like regular expressions, explodes are normally a great alternative.

 

$dob = explode("/", $dob);

 

if(count($dob) != 3){

// wrong format

}else {

// right format

}

 

This doesn't check to make sure the parts are a set length. It would probably be better to cast the parts as int's and check the value that it would be to just make sure they contained two numbers - both because the two numbers could be out of range, and because it seems weird to require leading zeros.

Link to comment
https://forums.phpfreaks.com/topic/63425-preg_match/#findComment-316572
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.