Jump to content

Archived

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

khhalid

isDate()

Recommended Posts

hi Guys,

 

I have html <input type = 'text'...> field where user can enter date. 

 

Is there a way to make sure a valid date has been entered and the format is 'dd'/mm/yyyy'.

 

Many thanks

Share this post


Link to post
Share on other sites

You could use regex:

 

if (preg_match('/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}$/', $date))
{
    echo 'valid date: ' . $date;
}

Share this post


Link to post
Share on other sites

There are a variety of ways to do this, you can use javascript to make sure an alert is shown that the date is not valid or you can use php/ajax to show an error once the form is submitted that the data is not valid.  What are you looking to do with this?

Share this post


Link to post
Share on other sites

Given the forum this is posted in, I'm going to assume he means PHP.

Share this post


Link to post
Share on other sites

I am using HTML with JavaScript (JS).  Any JS example will be very helpful, please. :D

Share this post


Link to post
Share on other sites

So you posted in the 'PHP coding help' section?

 

Okay then, well you can still use regex:

 

if (yourDate.match(/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}$/))
{
    // valid date
}

Share this post


Link to post
Share on other sites

Sorry MrAdam, I'm using HTML, JScript & PHP together.

I will try your solution, thanks;

Share this post


Link to post
Share on other sites

You should always check it again on server side anyway.

Share this post


Link to post
Share on other sites

You should always check it again on server side anyway.

Server-side validation = must have

Client-side validation = nice to have

Share this post


Link to post
Share on other sites

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