PHP-Verify Name, e-mail, and URL

Source: Internet
Author: User
Tags php form php form validation
The following is the source code:

<html><head><style> . Error{color:#FF0000;}   
     Style
 > 
     Head
 ><body>
    //define variables and set to empty values$NAMEERR=$EMAILERR=$GENDERERR=$WEBSITEERR="";$name=$email=$gender=$comment=$website="";if($_server["Request_method"] =="POST"){if(Empty($_post["Name"]))     {$NAMEERR="Name is required";}Else{$name= Test_input ($_post["Name"]);//Check if name only contains letters and whitespaceif(!preg_match ("/^[a-za-z]*$/",$name))       {$NAMEERR="Only letters and white space allowed"; }     }if(Empty($_post["Email"]))     {$EMAILERR="Email is required";}Else{$email= Test_input ($_post["Email"]);//Check if e-mail address syntax is validif(!preg_match ("/([\w\-]+\@[\w\-]+\.[ \w\-]+)/",$email))       {$EMAILERR="Invalid email format"; }     }if(Empty($_post["website"]))     {$website="";}Else{$website= Test_input ($_post["website"]);//Check if URL address syntax is valid (this regular expression also allows dashes in the URL)if(!preg_match ("/\b (?:(?: https?| FTP): \/\/|www\.) [-a-z0-9+&@#\/%?=~_|!:,.;] *[-a-z0-9+&@#\/%=~_|] /I ",$website))       {$WEBSITEERR="Invalid URL"; }     }if(Empty($_post["comment"]))     {$comment="";}Else{$comment= Test_input ($_post["comment"]);}if(Empty($_post["Gender"]))     {$GENDERERR="Gender is required";}Else{$gender= Test_input ($_post["Gender"]);}} functiontest_input($data){$data= Trim ($data);$data= Stripslashes ($data);$data= Htmlspecialchars ($data);return$data;}?><h2>PHP Form Validation Example 
      H2
 ><p><spanclass="error">* Required field. 
     span
 > 
     P
 ><formMethod="POST"Action="
 
      php_self "]);? >"> Name:<inputtype="text"name="name"><spanclass="error">*  
       echo$nameErr;? >
   
       span
  ><br><br>E-Mail:<inputtype="text"name="Email"><spanclass="error">*  
       echo$emailErr;? >
   
       span
  ><br><br>Website:<inputtype="text"name="website"><spanclass="error">  
       echo$websiteErr;? >
   
       span
  ><br><br>Comment:<textareaname="comment"rows="5"cols= "> " 
      textarea
 ><br><br>Gender:<inputtype="Radio"name="Gender"value ="female">Female<inputtype="Radio"name="Gender"value ="Male">Male<spanclass="error">*  
      echo$genderErr;? >
   
      span
  ><br><br><inputtype="Submit"name="Submit"value ="Submit"> 
     Form
 >
  
     Echo "

Your Input:

"; Echo $name; Echo "
"; Echo $email; Echo "
"; Echo $website; Echo "
"; Echo $comment; Echo "
"; Echo $gender; ?> body > HTML >

The above describes PHP-verify Name, e-mail, and URL, including the aspects of the content, want to be interested in the PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.