Post Validation

I have a website that when you submit two numbers i need to ensure that they both start correctly

how can i make it so if the first field say “from” must start with 07 and the to field must start 447 if one or both are incorrect i can echo a error message.

i know it will be an if statement but not sure how to do it as i need to make sure that when the details are posted they are correctly formatted.

im guessing it might be something like


// Check if numbers are correct

if (preg_match('/^44/', $from)) {

}

else if (preg_match('/^07/', $to)) {

}

what i would like it to do tho is if it’s true continue with the rest of the script but if it’s false then echo and error message

Yes, you are close:

if (!preg_match('/^44/', $to) || !preg_match('/^07/', $from))
{
  die('you failed the validation');
}

// Hey it passed if it gets down to here!

You can also use an else statement

if (!preg_match('/^447/', $to) || !preg_match('/^07/', $from))
{
  // echo "You failed the validation";
}
else
{
  // echo "You passed the validation";
}

cpradio once again a big thank you for your help.

i have tested it and it does echo what i want but how i can stop the script from continuing cause when i tested this it did echo what i wanted but continued with the script.

Are you able to post more of your script so I can see what context you used it in?

just threw


exit():

in to the if false part :slight_smile:

so now reads


// Check if the form has been inputted correctly
if (!preg_match('/^447/', $to) || !preg_match('/^07/', $from))
{
   echo "Please ensure you have entered the numbers correct";
   exit();
}
else
{
  // Continue
}