Page 1 of 1

PHP form validation to JS form validation

Posted: Wed Feb 17, 2010 10:15 am
by kevinkhan
Hi guys.. I really need a bit of help.. is anyone good looking at this good with JS?

I have a php form validation script but i think its a bit slow and would rather a JS script instead...

here is what i have in php..

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)
          {
          echo "<p class=\"formMessage\">";
          
          
            echo $error[0];
            
            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 class=\"formMessage\">Thanks for submitting your enquiry.</p>");
            }
            else
            {
            echo("<p class=\"formMessage\">Message delivery failed.</p>");
            }
          }
          else
          {
          //echo "error";
         display_errors($errors);
          }    
      }                   
?>
    <form id="form" method="post" action="index.php#quickContact"> 
              <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 class="send" type="image" src="images/submit2.gif" name="submit" value="Submit"  /></p> 
  </form>  
does anyone know how to transfer this to JS so that it will be easy to understand.. Im not good with JS at all :(

Re: PHP form validation to JS form validation

Posted: Wed Feb 17, 2010 8:17 pm
by josh