Jump to content

Recommended Posts

Well, your question wasn't very specific. What exactly are you checking? User input? A record stored in a database? A string?

 

 

Anyway...assuming it's a string or something manually entered in a form (Bad idea to let people enter dates without forcing the format yourself)....

 

<?php

if(!preg_match('/[0-3][0-9]\/[0-1][0-9]\/2[0][0-9][0-9]/', $date) Errors('date'); //Assuming you wrote an Errors() function.
  else echo 'Email is formatted correctly';

?>

Well, your question wasn't very specific. What exactly are you checking? User input? A record stored in a database? A string?

 

 

Anyway...assuming it's a string or something manually entered in a form (Bad idea to let people enter dates without forcing the format yourself)....

 

<?php

if(!preg_match('/[0-3][0-9]\/[0-1][0-9]\/2[0][0-9][0-9]/', $date) Errors('date'); //Assuming you wrote an Errors() function.
  else echo 'Email is formatted correctly';

?>

 

Not so fast there buddy. That regex would validate 39/19/2099.  Correct format but obviously a bunk date.  And what if this was some kind of birthday date form? Not everybody was born in the year 2000+

There would be no point in a vaidation then because they would only be able to use those values. Ok then no worries i think iw ill use Drop downs in that case because i can get it exactly how i want it :)

 

Always always ALWAYS validate input.

No, seriously.  Consider this simple form:

 

<form action='somepage.php' method = 'post'>
   <select name = 'blah'>
      <option value = '1'>1</option>
      <option value = '2'>2</option>
      <option value = '3'>3</option>
   </select>
   <input type = 'submit' value = 'submit'>
</form>

 

If you do not check to see if $_POST['blah'] is 1,2 or 3.  I can do:

 

Rightclick

view source

highlight and copy form

paste into my own html file

change one of the values to anything I want

change the action= from somepage.php to http://www.yoursite.com/somepage.php

save

load up my file in my browser

choose the selection with my altered data

submit

 

Your form takes it without validating, and you get wtfpwned. 

try

<?php
$test = '29/02/2001';
$test = explode('/', $test);
$comp = explode('/',date('d/m/Y',mktime(1,1,1,$test[1],$test[0],$test[2])));
if($test[0]==$comp[0] and $test[1]==$comp[1] and $test[2]==$comp[2]) echo 'OK'; else echo 'no';
?>

To test if a date is valid - http://us3.php.net/checkdate

 

yer but i dont wana write an if statement say if($VAUE != 1 || $VALUE != 2 etc etc

If you need to test that a value is or is not in a range of values, there are more ways to do a comparison besides == and !=

http://us.php.net/manual/en/language.operators.comparison.php

 

// example number
$x = 10;

// example 1
if (($x >= 1) && ($x <= 100)) {
  echo "$x is between 1 and 100...and I didn't have to write 100 comparisons!";
}

// example 2
if (in_array($x, range(1,100))) {
  echo "$x is between 1 and 100...and I didn't have to write 100 comparisons!";
}

 

you would use a condition, like an if statement or something.  That's pretty basic stuff.  There are lots of tutorials out there for php basics.

 

 

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.