0

Hi guys what is the proper coding of this if else statement if name midname last name email phone for required field and a validation.

if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
 die("First Name Should be letters and white space only!"); 
}
if (!preg_match("/^[a-zA-Z ]*$/",$miname))
{
die("Please Enter Your Middle Name!"); 
}
 if (!preg_match("/^[a-zA-Z ]*$/",$lastname))
{
  die("Please Enter Your Last Name!"); 
}
if (!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z] {2,3})$/i",$email))
{
  die("E-mail address not valid");
}
else if (!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i",$phone))
{
 die("Phone should only contain 11 digits and start with (09)!"); 
}


if($_POST["inputName"] &&$_POST["inputmiName"]&&$_POST["inputlName"] &&     $_POST["inputEmail"]&& $_POST["inputPhone"]&& $_POST["inputEvent"]&& $_POST["date"]){ $name=   $_POST["inputName"]; $miname= $_POST["inputmiName"]; $lastname= $_POST["inputlName"];     $email= $_POST["inputEmail"]; $phone= $_POST["inputPhone"]; $date= $_POST["date"];}
 else{ exit("Please Complete The Form!");}

   $sql = "INSERT INTO `catering` (`name`, `miname`,`lastname`,`email`, `phone`, `event`, `food`, `foodA`, `foodB`, `foodC`, `foodD`, `foodE`, `foodF`, `foodG`, `foodH`, `foodI`, `foodJ`, `pack1`,`total`,`date`, `date_join`) VALUES ( ' ". $name ." ' ,' ". $miname ." ' ,' ". $lastname ." ' ,' ". $email ." ' ,' ". $phone ." ' ,' ". $event ." ' ,' ". $foodvariable ." ' , ' ". $foodvariableA ." ', ' ". $foodvariableB ." ' , ' ". $foodvariableC ." ' , ' ". $foodvariableD ." ' , ' ". $foodvariableE ." ' , ' ". $foodvariableF ." ' , ' ". $foodvariableG ." ' , ' ". $foodvariableH ." ' , ' ". $foodvariableI ." ' , ' ". $foodvariableJ ." ' ,' ". $pack1 ." ' ,' ". $total ." ', ' ".  $date_new ." ' ,NOW());";
  if( mysqli_query($conn, $sql )) echo "Food Reservation SENT![Your Reservation Is only Valid for 3 days. after 3days we will call you to confirm if you will continue your reservation  Thank You!";

  ?>
1
  • 1
    is there any code after this validation? if so , don't use die because the script will end if any of the validation conditions become true Commented Mar 8, 2014 at 8:47

2 Answers 2

1

I didn't check your regex pattern but no, it's not.

This king of validation is not well suited in your appplications. Normally, every if statement should have an else and as many else-if-s as you want. It's not mandatory but is a good practice.

The second think I want to tell you is that you should use a class that takes care of your validation. Example:

static Class MyValidator {
    public static function AlphaString($str){
        return preg_match("/^[a-zA-Z ]*$/", $str)
    }
}

This is a good way to make validations. If you need changes, you can do them in only one place in your application.

The third thing I wanna say is that in WebApps it's good to use third party open-source software. In this case you can use jQuery Validation plugin. Of course you still need to test you code on the server too but at this part, using a framework may save you a lot of time. Any PHP framework has validators.

Sign up to request clarification or add additional context in comments.

1 Comment

While a good answer (I plus oned) and logic I would suggest this be avoided until you know a bit more about php (going off how you asked in my answer about how to implement my suggestions).
0

For the middle name and last name you don't need to use preg_match() you could just check if it were empty or not

So instead of:

else if (!preg_match("/^[a-zA-Z ]*$/",$miname))

You would use:

else if (strlen($miname) === 0)

You also don't need to use elseif statements. Simple if statements would do.

Also the preg_match on the phone number could be removed and replaced with a strlen() to check the length and a substr() to check the first 2 digits.

So instead of:

else if (!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i",$phone))

You would use:

else if (substr($phone, 0, 2) !== '09' || strlen($phone) !== 11)

Preg match is a very intensive function and should only be used sparingly.

6 Comments

thnx for your comment ill try it tnx there is a code after the validitation
can you teach me where should i put that codes? like strlen($miname)====0 and the substr and strlen i dont know where should i put it:))
do i need to delete the die? and in first name and last name i will not change it anymore?
last name can use the same code as midname, first name you are checking for alpha characters and whitespace so you can keep using preg_match() there. A die() will stop the rest of the script, so if one of those validations fails it won't check the others under it. I assume that is what you want. If so then you can keep them in.
but i am just wondering why this number is appearing in my localhost db, 2147483647 but i didnt inputted this number any number i put by 11digits still this number appears 2147483647
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.