PHP Basic (iv) Form validation

Source: Internet
Author: User
Tags php form

This article is from http://www.w3school.com.cn/php/php_form_url_email.asp

PHP Form Verification-Verify e-mail and URLs

This section shows you how to verify your name, email, and URL.

PHP-Validating names

The following code shows a simple way to check whether the Name field contains letters and spaces. If the name field is not valid, an error message is stored:

$name = Test_input ($_post["name"]); if (!preg_match ("/^[a-za-z]*$/", $name)) {  = "Only letters and spaces are allowed! "; }

Note: the Preg_match () function retrieves the pattern of a string and returns True if the pattern exists, otherwise false.

PHP-Verify e-mail

The following code shows a simple way to check whether the e-mail address syntax is valid. If it is not valid, an error message is stored:

$email = test_input ($_post["email"]); if (!preg_match ("/([\w\-]+\@[\w\-]+\.[ \w\-]+)/", $email)) {  =" Invalid email format! "; }

PHP-Validating URLs

The following code shows a method that checks whether the URL address syntax is valid (this regular expression also allows a slash in the URL). If the URL address syntax is invalid, an error message is stored:

$website = test_input ($_post["website"]); if (!preg_match/\b (?:(?: https?| FTP): \/\/|www\.) [-a-z0-9+&@#\/%?=~_|!:,.;] *[-a-z0-9+&@#\/%=~_|] /I ", $website))  {=" Invalid URL ";}

PHP Validation Example
<?php//defines the variable and sets it to a null value $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 the name contains letters and whitespace characters if (!preg_match ("/^[a-za-z]*$/", $name)) {$NAMEERR = "allow only letters and spaces";   }} if (Empty ($_post["email")) {$EMAILERR = "e-mail is required";     } else {$email = Test_input ($_post["email"]); Check that the e-mail address syntax is valid if (!preg_match ("/"/([\w\-]+\@[\w\-]+\.[      \w\-]+)/", $email)) {$EMAILERR =" Invalid email format ";   }} if (Empty ($_post["website")) {$website = "";     } else {$website = Test_input ($_post["website"]); Check that the URL address syntax is valid (the regular expression also allows a slash 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"]);   }}function Test_input ($data) {$data = Trim ($data);   $data = Stripslashes ($data);   $data = Htmlspecialchars ($data); return $data;}? ><?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type=" text "name=" name "> <span class=" Error ">*<?php echo $nameErr;?></span> <br><br> Email: <input type= "text" name= "email" > <span class= "Error" >*<?php echo $emailErr;?></span> <br><br> URL: <input type= "text" name= "website" > <span class= "Error" ><?php echo $websiteErr;?></span> <br><br> Comments: <textarea name= "comment" rows= "5" cols= "+" ></textarea> <br> <br> Gender: <input type= "Radio" name= "Gender" value= "female" > Female <input type= "Radio" name= "Gender" value= "Male" > Male <span class= "error" >*<?php echo $genderErr;?></span> <br><br> <input type= "Submit" name= "Submit" value= "Submission" > </form><?phpecho "</body>

PHP Basic (iv) Form validation

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.