Jump to content

Validation


user4fun

Recommended Posts

$name = $_REQUEST['name'];$t1 = $_REQUEST['t1'];$t2 = $_REQUEST['t2'];$t3 = $_REQUEST['t3'];$entry = $_REQUEST['entry'];$min_entry = "50000";//t1, t2, and t3 are the users phone number starting with area codeI need to validate that $name is not emptyother wise go to name_error.phpTHEN$t1 is not less OR more that 3 chracters 0 to 9Other wise go to ac_error.php$t2 is not less OR more that 3 characters 0 to 9 AND $t3 is not less OR more that 4 digits 0 to 9Other wise go to number_error.php$entry is not less than $min_entry and only using chracters 0 to 9If all the above is OK then //more codeIt is duable, except I don't know how to NOT repeat the 0 to 9 stipulation of number of chracters.Also, I am not sure if it would be if...else of elseifPlease help

Link to comment
Share on other sites

This is the low-tech way, without regular expressions. Regexp will give you a better match, but I've been drinking and I don't want to think about regexp right now.

$name is not emptyother wise go to name_error.php
if ($name == ""){  header("Location: name_error.php");  exit();}

$t1 is not less OR more that 3 chracters 0 to 9Other wise go to ac_error.php
if (strlen($t1) != 3 || intval($t1) != $t1){  header("Location: ac_error.php");  exit();}

$t2 is not less OR more that 3 characters 0 to 9 AND $t3 is not less OR more that 4 digits 0 to 9Other wise go to number_error.php
if (strlen($t2) != 3 || intval($t2) != $t2 || strlen($t3) != 4 || intval($t3) != $t3){  header("Location: number_error.php");  exit();}

$entry is not less than $min_entry and only using chracters 0 to 9
if ($entry < $min_entry || !preg_match('^\d+$', $entry)){  echo "bah!";}else{  // more code}

Damnit, you got me to use regexp.

Link to comment
Share on other sites

I dont understand this part?
If
$entry is not less than $min_entry and only using chracters 0 to 9
then
echo "bah!";

Link to comment
Share on other sites

No, I think boen_robot meant to sayIf

$entry is less than $min_entry or uses characters other than 0 to 9
then
echo "bah!";

So then the $entry is invalid.

Link to comment
Share on other sites

Ah wait using Regex it should be like this

if ($entry < $min_entry || preg_match('^\d+$', $entry)){echo "bah!";}

Link to comment
Share on other sites

This should work:

if ($entry < $min_entry || !preg_match('^\d+$', $entry)){  echo "the entry was less then the min entry, or it contained something other then digits";}else{  // more code}

The if statement says that if the entry is less then min_entry, or if it does not match the expression, then go to the first case. So that means the first case is the failure case. If you want the first case to be the successful case you can reverse the logic.

if ($entry >= $min_entry && preg_match('^\d+$', $entry)){  echo "the entry was longer then the minimum and contained only digits";}else{  // more code}

Link to comment
Share on other sites

This is exactly how I have itWhen i put ancd instead of numbers it gives me the errorif I put in 64574673654Which is larger that $min_entry which is 5000 it still gives me the error???

if ($entry < $min_entry || !preg_match('^\d+$', $entry)) {  header("Location: entry_error.php");  exit();}else{  echo "good";}

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...