PHP Form Validate Email and URL in Hindi – इस Tutorial में हम Email and Url validation करना सीखेंगे। जब Form में User Email और Url etc enter करता है। तो PHP ये Check कर सके ये सही Email और URL Enter की गयी है। या गलत है।
PHP Form Validate Email and URL in Hindi
PHP Form से Related अभी तक कई tutorial पढ़ चुके है। पिछले Tutorial में हमने PHP Form required in Hindi के बारे में पढ़ा था। जिसमे हमने ये सीखा था। Form field Input empty होने पे error कैसे Show करा सकते है। लेकिन इसमें हम Email and Url Validation के Code को देखेंगे।
PHP Email and URL Validation in Hindi
हम यहाँ पे Email URL and Mobile Number verification करने का पूरा Code देंगे। और समझायेंगे। जैसे पिछले Article में input field Empty था तो Error Show कराने के लिए If Else Statement का Use हुआ था। वैसे ही इसमें भी If Else के द्वारा ही Not validation पे Error Show करायेगे।
PHP E-mail Validation
PHP में Email को validate करने के कई तरीके है। लेकिन मै आपको ऐसा Code Share कर रहा हूँ। जिससे Easily Secure तरीके से PHP Email Validate कर सकते है।
<?php if (empty($_POST['email'])) { # code... $emailerr = "Please Enter Email Id*"; }else { $email = $_POST ["email"]; $pattern = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$^"; if (!preg_match ($pattern, $email) ){ $emailerr = "Email is not valid."; } else { $email = $_POST ["email"]; } } ?>
PHP URL Validation
<?php $websiteURL = $_POST["website"]; if (!preg_match("/b(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)) { $websiteErr = "URL is not valid"; echo $websiteErr; } else { echo "Website URL is: " .$websiteURL; } ?>
इसमें भी Regular Expression Pattern matching के द्वारा Check करेगा की http लगा है या www लगा है। अगर वो Url Validate नहीं है। तो Url is Not valid Display होगा।
PHP Mobile No Validate
<?php $mobileno = $_POST["mobileno"]; if (!preg_match ("/^[0-9]*$/", $mobileno) ) { $mobilenoerr = "Only numeric value is allowed."; } //check mobile no length should not be less and greator than 10 if (strlen ($mobileno) != 10) { $mobilenoerr = "Mobile no must contain 10 digits."; } else { $mobileno = $_POST["mobileno"]; } } ?>
<?php $name = $email = $comment = $website =$mobileno = $gender = ''; $nameerr = $emailerr = $commenterr = $websiteerr = $mobilenoerr = $gendererr = ''; if (isset($_POST['submit'])) { # code... if (empty($_POST['website'])) { # code... $websiteerr = "please enter URL"; }else { $website = $_POST['website']; if (!preg_match("/b(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)) { $websiteerr = "URL is not valid"; } else { $website = $_POST['website']; } } $comment = $_POST['comment']; if (empty($_POST['name'])) { # code... $nameerr = "Please Enter Your Name*"; }else{ $name = $_POST['name']; } if (empty($_POST['email'])) { # code... $emailerr = "Please Enter Email Id*"; }else { $email = $_POST ["email"]; $pattern = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$^"; if (!preg_match ($pattern, $email) ){ $emailerr = "Email is not valid."; } else { $email = $_POST ["email"]; } } if (empty($_POST['mobileno'])) { # code... $mobilenoerr = "Please Enter Mobile No*"; }else{ $mobileno = $_POST["mobileno"]; if (!preg_match ("/^[0-9]*$/", $mobileno) ) { $mobilenoerr = "Only numeric value is allowed."; } //check mobile no length should not be less and greator than 10 if (strlen ($mobileno) != 10) { $mobilenoerr = "Mobile no must contain 10 digits."; } else { $mobileno = $_POST["mobileno"]; } } if (empty($_POST['gender'])) { # code... $gendererr = "Select Gender*"; }else{ $gender = $_POST['gender']; } } ?> <!DOCTYPE html> <html> <head> <title>HTML Form</title> </head> <body> <h2>PHP Form Validation Example</h2> <form method="post"> Name: <input type="text" name="name"><br> <span style="color: red;"> <?= $nameerr; ?></span><br> E-Mail: <input type="text" name="email"><br> <span style="color: red;"> <?= $emailerr; ?></span><br> Website: <input type="text" name="website"><br><br> <span style="color: red;"> <?= $websiteerr; ?></span><br> Mobile No: <input type="text" name="mobileno"> <br> <span style="color: red;"> <?= $mobilenoerr; ?></span><br> <br><br> Comment: <input name="comment" rows="5" cols="40"><br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <input type="radio" name="gender" value="other">Other <br> <span style="color: red;"> <?= $gendererr; ?></span><br> <input type="submit" name="submit" value="Submit"> </form> <h2>Your Input</h2> <?php echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>