Posted By:
19 Oct, 2013 3:43 pm PDT

Validate a form in Php

How can i validate a form in before the form is submitted? There are several fields in the form:

  1. Name- should contain only alphabets and numbers
  2. Phone no- should be 10 digits
  3. Email
  4. Web addresss
Attachment:None


<?php

#for name

if($_POST['name']!=''){

    $aValid = array('-', '_',' ');

    if(ctype_alnum(str_replace($aValid, '', $_POST['name']))) {

 

    }

    else{

        echo '<script>alert('Incorrect Format')</script>';

    }

} 

#for phone...

if($_POST['phone']!=""){  

    if(preg_match("/^[0-9]{10}$/", $_POST['phone'])) {

    }

    else{

              echo '<script>alert('Incorrect Format')</script>';

     }

}

 

########Email#################

if($_POST['email']!=""){

    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

       

    }

    else{

             echo '<script>alert('Incorrect Format')</script>';

    }

}

 

 

########Weblink#################

 

if($_POST['web']!=""){

  

    if(filter_var($_POST['web'], FILTER_VALIDATE_URL)){    

    }

    else{

   

             echo '<script>alert('Incorrect Format')</script>';

 

    }

}

?>

 


Was it useful?  




Please login to reply to this problem.