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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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