0

I'm trying to validate several form items using PHP, including a full name, a phone number, an email address, a checkbox and a radio button set. I've started with email (using FILTER_VALIDATE_EMAIL) and phone (using a regular expression), but have come across several errors that I can't figure out. Below is the code for the PHP for both validation functions and the corresponding HTML:

PHP:

<?php
$phone_validate = "";
$email_validate = "";
$phone_expr = '^\(?([0-9]{3})\)?[-]?([0-9]{3})[-]?([0-9]{4})$';

function validatePhone ($input){
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if (preg_match($phone_expr, $input) == 1){
        return "This phone is valid.";
      }
      else {
        return "This phone is not valid.";}
  }
}

function validateEmail ($input){
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if ((!filter_var($input, FILTER_VALIDATE_EMAIL))){
      return "This email is valid.";}
    else {
      return "Please enter a valid email.";}
  }
}
?>

HTML:

    <div class="contactForm">
      <label for="phone">Phone: </label>
      <div id="errorphone" class="error"></div>
      <input type="phone" name="phone" id="phone" placeholder="000-000-0000" required>
      <p class="valid"><?= validatePhone($_POST['phone']);?></p> 
    </div>

    <div class="contactForm">
      <label for="email">Email*: </label>
      <div id="erroremail" class="error"></div>
      <input type="email" name="email" id="email" placeholder="janedoe@email.com" required>
      <p class="valid"><?= validateEmail($_POST['email']);?></p>
    </div>

When I run it I get these error messages.

enter image description here

Can anyone give me some pointers on what is causing these errors, or a better method to achieve the validation?

Sarah Diri
  • 97
  • 1
  • 9

1 Answers1

-1

u can use this code -> go

after you download that, i will give you example on below

$name = $_GET['name']

$validateName = Sanitizer::clean($name, "/^[a-zA-Z0-9\s]+$/", 1, 20);

if($validateName->status == 1){

    echo $validateName->value;

}

how it works?

each you will validate or clean variable input, you will call static function name Sanitizer with four parameter's, Sanitizer::clean($value, $regularExpression, $minlength, $maxlength), you can change each variable what you want.

after call a function, it gives return object type, the object is status $validateName->status and value $validateName->value.

If validateName meet the requirements of a regular expression, will return two object, each object is $validateName->status, and $validateName->value. beacuse it valid input, $validateName->status always 1. and $validateName->value it will be kept.

if $validateName does not meet the requirements, it will return only one object, it is $validateName->status, and status will 0. and no object $validateName->value.

In essence, if you get a value, it means that the input and regular expression that you entered match.

meotig
  • 76
  • 5