SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict clearweb's Avatar
    Join Date
    Dec 2001
    Location
    Southern California
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Email and Phone Validations for form - but not the typical validations

    I have an html form.
    It goes to a "send.php" page for processing.
    In my form, I want it to confirm the proper format for both the email address and the phone number (ex: 555-555-555 and johndoe@domain.com). However, if the format is okay, I want it to continue and process the form.

    I have noticed that many of these "email validation" scripts are to either echo a "email address is valid" or a "email address is not valid".

    I need my script to check the format. If it is valid, then continue processing the form. If it's not valid, then have it echo "email address is not valid.

    I have racked my brain trying to figure this out. I have Googled all of this stuff and can't find the right information. Here is a copy of my code. I also have some other "if" statements, so I don't know what I will have to do in order to make this work right. Obviously, I am new at this, but I am trying really hard to figure this out. Any help is appreciated:

    PHP Code:
    <?php
    $to 
    "name@domain.com";
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $address $_POST['address'];
    $aptsuite $_POST['aptsuite'];
    $city $_POST['city'];
    $state $_POST['state'];
    $zipcode $_POST['zipcode'];
    $email $_POST['email'];
    $homephone $_POST['homephone'];
    $workphone $_POST['workphone'];
    $cellphone $_POST['cellphone'];
    $sub "Information Request";
    $headers "From: $email <$email>\n"
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
    $mes .= "First Name: ".$firstname."\n" ;
    $mes .= "Last Name: ".$lastname."\n" ;
    $mes .= "Address: ".$address."\n";
    $mes .= "Apt/Suite: ".$aptsuite."\n";
    $mes .= "City: ".$city."\n";
    $mes .= "State: ".$state."\n";
    $mes .= "Zip Code: ".$zipcode."\n";
    $mes .= 'Email: '.$email."\n";
    $mes .= 'Home Phone: '.$homephone."\n";
    $mes .= 'Work Phone: '.$workphone."\n";
    $mes .= 'Cell Phone: '.$cellphone."\n";
     
    if (empty(
    $firstname) || empty($lastname) || empty($address) || empty($city) || empty($state) || empty($zipcode) || empty($email))
    {
    echo 
    " <h3>Sorry, fields with an asterisk are required.</h3>";
    }
    elseif (empty(
    $homephone) && empty($workphone) && empty($cellphone)) 

    echo 
    " <h3>At Least One Phone Number must be entered.</h3>";

    else
    {
    mail($to$sub$mes$headers);
    echo 
    "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.html\">";
    }
    ?>

  2. #2
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have found validation scripts on the net, then they should be easy to adapt for your form, just insert your code where they echo "The email is valid".
    For validation, the best technique is to use regular expressions, where you match the input against a certain pattern of characters. If you don't know how to use these then look them up on Google, there are plenty of tutorials out there.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  3. #3
    SitePoint Addict clearweb's Avatar
    Join Date
    Dec 2001
    Location
    Southern California
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bigalreturns View Post
    If you have found validation scripts on the net, then they should be easy to adapt for your form, just insert your code where they echo "The email is valid".
    For validation, the best technique is to use regular expressions, where you match the input against a certain pattern of characters. If you don't know how to use these then look them up on Google, there are plenty of tutorials out there.
    This is the problem I am having. I don't want the code to echo "the email is valid". I want it to process the form if it is valid, and send the email. I only want it to echo if the email is not valid. I am not experienced enough in PHP to know how to manipulate these scripts to do this.

  4. #4
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I found a validation script on the net, and I'll show you how to implement it. The script I've got is this...
    PHP Code:
    function check_email_address($email) {
      
    // First, we check that there's one @ symbol, and that the lengths are right
      
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$"$email)) {
        
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        
    return false;
      }
      
    // Split it into sections to make life easier
      
    $email_array explode("@"$email);
      
    $local_array explode("."$email_array[0]);
      for (
    $i 0$i sizeof($local_array); $i++) {
    if (!
    ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$"$local_array[$i])) {
          return 
    false;
        }
      }  
      if (!
    ereg("^\[?[0-9\.]+\]?$"$email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
        
    $domain_array explode("."$email_array[1]);
        if (
    sizeof($domain_array) < 2) {
            return 
    false// Not enough parts to domain
        
    }
        for (
    $i 0$i sizeof($domain_array); $i++) {
          if (!
    ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$"$domain_array[$i])) {
            return 
    false;
          }
        }
      }
      return 
    true;

    It's not worth worrying with the exact workings of the script at this moment, all you need to know is this. It creates a function called check_email_address , that will take an email address, check it, then return true if its valid, or false if it's not. What you now need to do is write some code that will call this function, sending it the entered email address, then if its invalid, echo "Not a valid email", whereas if it is valid, process the form and send the mail. This code would look something like this...

    PHP Code:
    IF (check_email_address($email)) {
    //your mailer code here
    } ELSE {
    echo (
    "This email is not valid");

    The if statement is the key here. When PHP reaches this part it tells check_email_address to run, using the email that is in the brackets after the function name. If its valid, then the function sends back the value true, so essentially it would become IF (true), which is obviously true. If it's not valid, then it sends back IF(false), so PHP will go to the ELSE part of the statement.
    So to integrate the IF code just discussed with the original validator function, we get something like this...

    PHP Code:
    function check_email_address($email) {
      
    // First, we check that there's one @ symbol, and that the lengths are right
      
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$"$email)) {
        
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        
    return false;
      }
      
    // Split it into sections to make life easier
      
    $email_array explode("@"$email);
      
    $local_array explode("."$email_array[0]);
      for (
    $i 0$i sizeof($local_array); $i++) {
     if (!
    ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$"$local_array[$i])) {
          return 
    false;
        }
      }  
      if (!
    ereg("^\[?[0-9\.]+\]?$"$email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
        
    $domain_array explode("."$email_array[1]);
        if (
    sizeof($domain_array) < 2) {
            return 
    false// Not enough parts to domain
        
    }
        for (
    $i 0$i sizeof($domain_array); $i++) {
          if (!
    ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$"$domain_array[$i])) {
            return 
    false;
          }
        }
      }
      return 
    true;
    }
    // first of all we must define the function
    // then we get the variables from the form
    $to "name@domain.com";
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $address $_POST['address'];
    $aptsuite $_POST['aptsuite'];
    $city $_POST['city'];
    $state $_POST['state'];
    $zipcode $_POST['zipcode'];
    $email $_POST['email'];
    $homephone $_POST['homephone'];
    $workphone $_POST['workphone'];
    $cellphone $_POST['cellphone'];
    $sub "Information Request";
    $headers "From: $email <$email>\n"
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
    $mes .= "First Name: ".$firstname."\n" ;
    $mes .= "Last Name: ".$lastname."\n" ;
    $mes .= "Address: ".$address."\n";
    $mes .= "Apt/Suite: ".$aptsuite."\n";
    $mes .= "City: ".$city."\n";
    $mes .= "State: ".$state."\n";
    $mes .= "Zip Code: ".$zipcode."\n";
    $mes .= 'Email: '.$email."\n";
    $mes .= 'Home Phone: '.$homephone."\n";
    $mes .= 'Work Phone: '.$workphone."\n";
    $mes .= 'Cell Phone: '.$cellphone."\n";

    // now we have the function defined, and the variables assigned, we can run the if statement

    IF (check_email_address($to)) {
    mail($to$sub$mes$headers);
    echo 
    "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.html\">";
    } ELSE {
    echo (
    "This email is not valid");

    This final chunk of code should work for you. As a note, you should not use direct user input in your code, due to the danger of so called "injection attacks," where malicious users can insert their own code into the script, that could then be executed. Basically, to prevent this, you need to make sure that any ' or " in the users input is escaped, ie has a \ in front of it. You can do this using the PHP function addslashes($userinput);. Unfortunately, some PHP installs do this automatically, so if you do it again you will end up with double backslashes! To get round this, I create this function at the top of the page, and then use it whenever I retrieve user input. All it does is detect if PHP is going to automatically add the slashes, and only add them if it isn't.

    PHP Code:
    function safeinput($input) {
    if (!
    get_magic_quotes_gpc()) {
       
    $input addslashes($input);
    }
    return 
    strip_tags($input);

    You would then retrieve user data with this

    PHP Code:
    $firstname=safeinput($_POST['firstname']); 
    and so on.

    Hope this has cleared some things up for you, if you have any questions then feel free to ask.

    Validation script was from http://www.ilovejackdaniels.com/php/...ss-validation/ originally by the way, guess it's nice credit them.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  5. #5
    SitePoint Addict clearweb's Avatar
    Join Date
    Dec 2001
    Location
    Southern California
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bigalreturns - thank you so much for the help! I don't know how much time I have put into this, but you have definitely put an end to my misery. I tried the code and it works like a charm!

    Thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •