PHP contact form phone validation of the correct amount of numbers
<p>PHP contact form phone validation of the correct amount of numbers</p>

<p>Hello,</p>

<p>I have this php form that validates the content once submitted a sticky php form is what it is called.  It keeps the users data in the input box when an error if found so the user dose not have to re-enter all the data again.</p>

<p>When the phone number is submitted I need it to validate that there are 3 characters/numbers in the first input box then 3 in the next then 4 in the last one. 
The way it is now as long as you input numbers in the first input box it over looks the rest of the input boxes for the phone number.  So I am looking to add a minimum character/number script in the validation process.  I have the form validating that it is a number at this time. I also need it to validate that there is the correct amount of numbers in each input box for the phone as well.  I believe this is just changing the elseif statements to just if inside another if but that did not work either.  Any help would be very appreciated.  The Art Institute only taught so much with PHP, and not this.</p>

<p>This is the particular area of the script that validates the phone number:</p>

<pre><code>//validate the phone number
if(is_numeric($_POST['phone01'])) { 
        $phone = $_POST['phone01']. '-';

}elseif(is_numeric($_POST['phone02'])) {    
        $phone .= $_POST['phone02']. '-';   

}elseif(is_numeric($_POST['phone03'])) { 
        $phone .= $_POST['phone03'];
}else{  
    print '&lt;p class="error"&gt;Please enter your Phone Number as 10 Number.&lt;/p&gt;';
    $validate = FALSE;
}
</code></pre>

<p>This is a copy of the whole script for the form itself:</p>

<pre><code>&lt;?php 
// This page receives the data from itself and validates as well

//error reporting!
ini_set ('display_errors', 1);

//Shows all possible problem!
error_reporting (E_ALL);

// validate email  
function isValidEmail($email){
    return eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email);
}


//show form
function show_form($firstName='',$lastName='',$businessName='',$email='',$phone01='',$phone02='',$phone03='',$message=''){

?&gt;

&lt;!--The form starts here --&gt; 

      &lt;form action ="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"  method="post" name="contact form" target="_self" id="contact form" dir="ltr" &gt;

        &lt;table bgcolor="#000000" width="525" border="0" align="center"&gt;
          &lt;tr&gt;
            &lt;td width="25%" align="right"&gt;*First Name:&lt;/td&gt;
            &lt;td colspan="2" align="left"&gt;&lt;input name="firstName" type="text" id="firstName" tabindex="1" size="30" value="&lt;?php if(isset($_POST['firstName'])) { print htmlspecialchars($_POST['firstName']); }?&gt;"/&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="right"&gt;*Last Name:&lt;/td&gt;
            &lt;td colspan="2" align="left"&gt;&lt;input name="lastName" type="text" id="lastName" tabindex="2" size="30" value="&lt;?php if(isset($_POST['lastName'])) {print htmlspecialchars($_POST['lastName']); }?&gt;"/&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="right"&gt;Business Name:&lt;/td&gt;
            &lt;td colspan="2" align="left"&gt;&lt;input name="businessName" type="text" id="businessName" tabindex="3" size="35" value="&lt;?php if(isset($_POST['businessName'])) {print htmlspecialchars($_POST['businessName']); }?&gt;"/&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="right"&gt;*Email: &lt;/td&gt;
            &lt;td colspan="2" align="left"&gt;&lt;input name="email" type="text" id="email" tabindex="4" size="35" value="&lt;?php if(isset($_POST['email'])) {print htmlspecialchars($_POST['email']); }?&gt;"/&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="right"&gt;*Phone Number:&lt;/td&gt;
            &lt;td colspan="2" align="left"&gt;
              &lt;input name="phone01" type="text" id="phone01" size="3" maxlength="3" tabindex="5"value="&lt;?php if(isset($_POST['phone01'])) {print htmlspecialchars($_POST['phone01']); }?&gt;"/&gt;
              - &lt;input name="phone02" type="text" id="phone02" size="3" maxlength="3" tabindex="6"value="&lt;?php if(isset($_POST['phone02'])) {print htmlspecialchars($_POST['phone02']); }?&gt;"/&gt;
              - &lt;input name="phone03" type="text" id="phone03" size="4" maxlength="4" tabindex="7" value="&lt;?php if(isset($_POST['phone03'])) {print htmlspecialchars($_POST['phone03']); }?&gt;"/&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;tr align="center"&gt;
            &lt;td align="right"&gt;*Message:&lt;/td&gt;
            &lt;td colspan="2" align="left"&gt;&lt;textarea name="message" type="text" id="message" tabindex="8" cols="45" rows="4"&gt;&lt;?php if(isset($_POST['message'])) {print htmlspecialchars($_POST['message']); }?&gt;&lt;/textarea&gt;
            &lt;/td&gt;
            &lt;/tr&gt;
          &lt;tr align="center"&gt;
            &lt;td&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td&gt;&lt;input name="submit" type="submit" tabindex="9" value="Email" /&gt;&lt;/td&gt;
            &lt;td&gt;&lt;input type="reset" name="reset" id="reset" value=" Reset " tabindex="10"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt; 
&lt;?php 
} // end of show_form function
$validate = TRUE;


if($_SERVER['REQUEST_METHOD']!='POST') {

  show_form();

  } else {

    //validate form fields

    //validate the first name
    if(empty($_POST['firstName'])) {
            print '&lt;p class="error"&gt;Please enter your First Name.&lt;/p&gt;';
            $validate = FALSE;
    }

    //validate the last name
    if(empty($_POST['lastName'])) {
            print '&lt;p class="error"&gt;Please enter your Last Name.&lt;/p&gt;';
            $validate = FALSE;
    }

    //validate the enail with email arrary
    if(!isValidEmail($_POST['email'])) {
            print '&lt;p class="error"&gt;Please enter your Email Address in the correct formate.&lt;/p&gt;';
            $validate = FALSE;
    }

    //validate the phone number
    if(is_numeric($_POST['phone01'])) { 
            $phone = $_POST['phone01']. '-';

    }elseif(is_numeric($_POST['phone02'])) {    
            $phone .= $_POST['phone02']. '-';   

    }elseif(is_numeric($_POST['phone03'])) { 
            $phone .= $_POST['phone03'];
    }else{  
        print '&lt;p class="error"&gt;Please enter your Phone Number as 10 Number.&lt;/p&gt;';
        $validate = FALSE;
    }

    //validate the message
    if(empty($_POST['message'])) {
            print '&lt;p class="error"&gt;Please enter your Messagee.&lt;/p&gt;';
            $validate = FALSE;
    }

    if(!$validate){
        print "&lt;p&gt;Please fill in all the fields with an asterisk * next to it and than please try again!&lt;/p&gt;";  

        show_form($_POST['firstName'],$_POST['lastName'],$_POST['businessName'],$_POST['email'],$_POST['phone01'],$_POST['phone02'],$_POST['phone03'],$_POST['message']);

    }else{
$phone01 = $_POST['phone01'];
$phone02 = $_POST['phone02'];
$phone03 = $_POST['phone03'];       
$phone = $phone01.'-'.$phone02.'-'.$phone03;        

    //confirmation email to client includes all information provided
    mail($_POST['email'], 'Contact Confirmation from www.Ozbar.net Web site', 'Thank You '.$_POST['firstName'].' '.$_POST['lastName'].' for your request for us to contact you. 

    Below is the information your provided us to contact you per your request.
    First Name: '.$_POST['firstName'].' 
    Last Name: '.$_POST['lastName'].'
    Business Name:  '.$_POST['businessName'].'
    Email Address: '.$_POST['email'].'
    Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].' 
    Message: '.$_POST['message'].' ','From:contact@steveoatman.me); 

    //notice of a new contact request
    mail('contact@steveoatman.me, 'Contact Request from www.Steveoatman.me Web site', ' 
    First Name: '.$_POST['firstName'].' 
    Last Word: '.$_POST['lastName'].'
    Business Name:  '.$_POST['businessName'].'
    Email Address: '.$_POST['email'].'
    Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].' 
    Message: '.$_POST['message'].' ','From:contact@steveoatman.me);

    print '&lt;p align="center"&gt;Thank You For Your Request!&lt;/p&gt;'?&gt;&lt;br /&gt;&lt;?php
    print '&lt;p align="center"&gt;We will contact you back with in 24-48 hours.&lt;/p&gt;' 
?&gt;
&lt;br /&gt;&lt;br /&gt; &lt;!--  if all validated a thank you statement --&gt;
&lt;?php
}

} //end of IF submit
// end of all php
?&gt; 


&lt;!-- end of #ref form --&gt;
</code></pre>
