Page 1 of 1

Form Validation Help

Posted: Wed Feb 17, 2010 6:11 am
by kevinkhan
Im trying to make a script that will only allow use full information in a form on my website... Here is the code i have so far...

Code: Select all

<?php
  if(isset($_POST['submit']))
      {
          $name = $_GET['name'];
          $email = $_POST['email'];
          $mobile = $_POST['mobile'];
          $comments = $_POST['comments'];
 
          if(empty($errors))
          {
            $to = "info@eventpromotion.ie";
            $subject = "Event Promotion Enquiry!";
            $body = 
             "First Name: " . $_POST['name'] .
             "\nEmail: " . $_POST['email'] .  
               "\nMobile: " . $_POST['mobile'] . 
             "\nMessage: " . $_POST['comments'];
           if (mail($to, $subject, $body)) {
            echo("<p>Thanks for submitting your enquiry.</p>");
            }
            else
            {
            echo("<p>Message delivery failed.</p>");
            }
          }
          else
          {
          echo "<p>".$error."</p>";
          }    
      }                   
?>
    <form id="form" method="post" action="testing.php"> 
              <p> 
                <label>Name</label><br /> 
                <input type="text" name="name" id="firstName" />      
            </p> 
              <p> 
                <label>Email:</label><br /> 
                <input type="text" name="email" id="email" /> 
                
            </p>               
            <p> 
                <label>Mobile:</label><br /> 
                <input type="text" name="mobile" id="mobile" /> 
                
            </p>                                  
            <p> 
                <label>Comments:</label> <br />
                <textarea name="comments" cols="30" rows="3"></textarea>    
            </p> 
            <p>    <input type="submit" name="submit" value="Submit"  /></p> 
  </form>  
I only want the form to be processed if the form fields are correctly filled out..

for name i want something like the input to be two words of a minimum of 3 charaters each maybe.. and if the user has not put this i want the script to display an error above the form saying something like "Please insert your full name with a space between your first and last name"

for the email field i would like a correctly formatted email address to be used and again if there is an error i would like the message to say something like "Please enter your correct email address"

For the phone field i would like the user to only enter a ten digit number

and for the comments maybe have a minimum of 20 chars..

Can somebody help me with this its bugging me all morning and i cant figure out the logic in it..

Thanks for your help...

Re: Form Validation Help

Posted: Wed Feb 17, 2010 6:26 am
by papa
In php is not hard to process that information after the user have submitted the form. However if you want it to validate during form input you have to use javascript.

For the name, you could make it easier for the user by supplying two fields, firstname and lastname. And then merge these two variables together adding a space. If you prefer not you use regular expression.

email: regular expression. It's pretty easy to find on google if you don't know how to do this yourself.

For telephone number you have is_number() and strlen()

comments: strlen()

Re: Form Validation Help

Posted: Wed Feb 17, 2010 6:55 am
by rahulzatakia
Hi,
I have written the following code with all your requirements...


<?php
session_start();

if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$comments = $_POST['comments'];
$flag = 0;

$name = array();
$name = explode(" ", $_POST['name']);

if(strlen($name[0]) < 3 || strlen($name[1]) < 3 || count($name) != 2)
{
$_SESSION['errname'] = "Please insert your full name with a space between your first and last name";
$flag = 1;
}

$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if(!eregi($pattern, $_POST['email']))
{
$_SESSION['erremail'] = "Please enter your correct email address";
$flag = 1;
}

$pattern = "^[0-9]*$";

if(!eregi($pattern, $_POST['mobile']))
{
$_SESSION['errmobile'] = "Please enter your correct mobile number";
$flag = 1;
}

$commentlen = strlen($_POST['comments']);
if($commentlen < 20)
{
$_SESSION['errcomments'] = "Comments should of minimum 20 characters...";
$flag = 1;
}

if($flag == 0)
{
$to = "info@eventpromotion.ie";
$subject = "Event Promotion Enquiry!";
$body =
"First Name: " . $_POST['name'] .
"\nEmail: " . $_POST['email'] .
"\nMobile: " . $_POST['mobile'] .
"\nMessage: " . $_POST['comments'];
if (@mail($to, $subject, $body)) {
echo("<p>Thanks for submitting your enquiry.</p>");
}
else
{
echo("<p>Message delivery failed.</p>");
}
}
}
?>
<form id="form" method="post" action="">
<p>
<label>Name</label><br />
<font color="#FF0000"><?php print $_SESSION['errname']; unset($_SESSION['errname']); ?></font><br>
<input type="text" name="name" id="firstName" />
</p>
<p>
<label>Email:</label><br />
<font color="#FF0000"><?php print $_SESSION['erremail']; unset($_SESSION['erremail']); ?></font><br>
<input type="text" name="email" id="email" />

</p>
<p>
<label>Mobile:</label><br />
<font color="#FF0000"><?php print $_SESSION['errmobile']; unset($_SESSION['errmobile']); ?></font><br>
<input type="text" name="mobile" id="mobile" maxlength="10" />

</p>
<p>
<label>Comments:</label> <br />
<font color="#FF0000"><?php print $_SESSION['errcomments']; unset($_SESSION['errcomments']); ?></font><br>
<textarea name="comments" cols="30" rows="3"></textarea>
</p>
<p> <input type="submit" name="submit" value="Submit" /></p>
</form>

Re: Form Validation Help

Posted: Wed Feb 17, 2010 6:58 am
by rahulzatakia
Hi kevinkhan,

Give reply as the code is working properly or not which I have posted for you...

Re: Form Validation Help

Posted: Wed Feb 17, 2010 7:00 am
by kevinkhan
ok just testing thanks for your help

Re: Form Validation Help

Posted: Wed Feb 17, 2010 7:07 am
by kevinkhan
every thing is working but the mobile field

http://www.corkdesigns.ie/testing/testing1.php

This mad.. would it be better too us js for this kind of job do you think?

Re: Form Validation Help

Posted: Wed Feb 17, 2010 9:09 am
by kevinkhan
Thanks for your help guys. I came up with this code in the end

Code: Select all

<?php
 
 
 
  if(isset($_POST['submit']))
      {
          $firstName = $_POST['firstName'];
          $lastName = $_POST['lastName'];
          $email = $_POST['email'];
          $mobile = $_POST['mobile'];
          $comments = $_POST['comments'];
         
          $errors = array();
          
        function display_errors($error_array)
          {
          echo "<p class=\"errors\">";
          
          foreach($error_array as $error)
            {
            echo $error . "<br />";
            }
            echo "</p>";
          }  
          
         function validateNames($names) 
        {
        return(strlen($names) < 3);
        } 
        
        function validateEmail($strValue) 
        {
           $strPattern = '/([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})/sim';
           return(preg_match($strPattern,$strValue));
        } 
        
        function validateMobile($strValue) 
        {
           $strPattern = '/^\d{10}$/';
           return(preg_match($strPattern,$strValue));
        } 
        
        function validateComments($comments) 
        {
        return(strlen($comments) < 10);
        } 
        
        if(validateNames($firstName)) 
        {    
        $errors[] = 'Please Enter Your First Name';    
        }
        
         if(validateNames($lastName)) 
        {    
        $errors[] = 'Please Enter Your Second Name';    
        }
        
          
        if(!validateEmail($email)) 
        {    
        $errors[] = 'Please Enter Your Correct Email';    
        }
        
        if(!validateMobile($mobile)) 
        {    
        $errors[] = 'Please Enter Your Correct Mobile Number';    
        }
        
        if(validateComments($comments)) 
        {    
        $errors[] = 'Please Enter A Comment More Than 10 Characters';    
        }
 
 
          if(empty($errors))
          {
            $to = "info@eventpromotion.ie";
            $subject = "Event Promotion Enquiry!";
            $body = 
             "First Name: " . $_POST['firstName'] .
             "\nLast Name: " . $_POST['lastName'] .
             "\nEmail: " . $_POST['email'] .  
               "\nMobile: " . $_POST['mobile'] . 
             "\nMessage: " . $_POST['comments'];
              $headers = "From: ". $firstName ." ". $lastName . " <" . $email . ">\r\n";
 
 
          if (mail($to, $subject, $body, $headers)) {
            echo("<p>Thanks for submitting your enquiry.</p>");
            }
            else
            {
            echo("<p>Message delivery failed.</p>");
            }
          }
          else
          {
          //echo "error";
         display_errors($errors);
          }    
      }                   
?>
    <form id="form" method="post" action="testing.php"> 
              <p> 
                <label>First Name</label><br /> 
                <input type="text" name="firstName" value="<?php if(isset($firstName)){echo $firstName;} ?>" />      
            </p> 
            <p> 
                <label>Last Name</label><br /> 
                <input type="text" name="lastName" value="<?php if(isset($lastName)){echo $lastName;} ?>" />      
            </p> 
              <p> 
                <label>Email:</label><br /> 
                <input type="text" name="email" value="<?php if(isset($email)){echo $email;} ?>" /> 
                
            </p>               
            <p> 
                <label>Mobile:</label><br /> 
                <input type="text" name="mobile" value="<?php if(isset($mobile)){echo $mobile;} ?>" /> 
                
            </p>                                  
            <p> 
                <label>Comments:</label> <br />
                <textarea name="comments" cols="30" rows="3" ><?php if(isset($comments)){echo $comments;} ?></textarea>    
            </p> 
            <p>    <input type="submit" name="submit" value="Submit"  /></p> 
  </form>  
 
 
if there are more than one error how do i limit to displaying only one error at a time...

do i need a different loop or how would i go about doing it does anyone know?

Re: Form Validation Help

Posted: Wed Feb 17, 2010 9:24 am
by papa
You mean if you have 10 errors, only show one and then let the user click on a "next" button to show error #2?

Re: Form Validation Help

Posted: Wed Feb 17, 2010 11:22 pm
by Weiry
If you don't have to use PHP, i would recommend using jquery for something like this.
It will check the form field as soon as you have made changes.
jQuery Form Validation