Learn the steps to validate email and URL validation in PHP

Email and URL validation in PHP is the first step in securing a site. Email and URL validation rules are easy in PHP and very practical to learn.

Do not miss today's fascinating topic! We are going to talk exactly about email and URL validation in PHP. This has been discussed many times and it may be best to make a serious decision about the need for email and URL validation in PHP.

Stay with us.  

Email and URL validation terms in PHP  

First we want to take a closer look at what email validation means. Keep in mind that in forms created with the php programming language, a series of information must be entered as input. Well, the important thing in this path is the information that is entered as input. In fact, you have to declare a series of conditions to determine whether the existing inputs are valid or not, for example, you can only allow the user to enter uppercase and lowercase letters and otherwise receive an error message. . Coding is needed to verify the validity of the input information, so we start coding. Take email and URL validation seriously in PHP.

  1. $name = test_input($_POST["name"]);  
  3. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {  
  5.   $nameErr = "You are only allowed to use uppercase and lowercase Latin letters and spaces";   
  7. }  

Pay attention to the written code. In this code, an input function is defined, and then a condition bracket is used, which simply states the conditions that are needed inside the condition.  

If this code snippet seems complicated to you, the next section provides the exact explanation needed in this path.

Email and URL validation coding analysis in PHP
Before reading the description of this section, you should review the previous code snippet. In this example, we first set a value for the Name field and then put the value inside $ name to make it easier to access.

Then it was the turn of the! Preg_match function, which actually checks to see if the content is within the specified range. For example, here the stipulation is uppercase, lowercase and space letters. If this is not limited, the if condition statement will be executed and you will be shown phrases written in the code and in fact you will be notified that you do not qualify. This value is placed in the #nameErr variable.
The preg-match function receives two parameters that are input. First we have to determine the desired range and then we have to look for the desired field. In the meantime, if the string matches the range, use the true option, otherwise the false option must be used. We found that the basics of email and URL validation in PHP are very easy. Just follow the email and URL validation rules in PHP properly.

The fastest way to validate email and URLs in PHP Now we want to go to a fast and practical way to validate email and URL in PHP so that we can get rid of a lot of coding fatigue and achieve our result and goal quickly. One of the fastest ways to validate email and URLs in PHP is the filter_var function, which specifically checks the original email format and lets you know if you did it correctly, otherwise This also launches the false tool. Note the coding of this part of email and URL validation in PHP:

  1. $email = test_input($_POST["email"]);  
  3. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {  
  5.   $emailErr = "Invalid email format";   
  7. }  

In this coding, first it is checked whether the field is full or empty and after checking this issue, we enter the filter_var function. In this function, the applied condition is checked exactly, and if the information entered is from the email format. Do not follow, it is declared that the format of the entered email is incorrect. Validating email and URLs in PHP is much easier this way than the previous one.

Email and URL validation tutorial in PHP field containing url  

We are at your service by teaching you email and URL validation in PHP, this time we want to talk about the part that actually examines the field containing the url. Note that in this section "-" is also valid and is actually checked. We specify the subject by coding:

  1. $website = test_input($_POST["website"]);  
  3. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {  
  5. $websiteErr = "Invalid url";  
  7. }  

Validating email and URLs in PHP is not that difficult, just get started.


In this article, we explained in simple language that validating mail and URLs in PHP is not as difficult as they say, it is enough to be logical in choosing the desired condition. We suggest you check the code snippet with different terms so that you know what is the most logical option for a user's input.


Email Validation Email and URL Validation Terms in PHP Email and URL Validation Coding in PHP Email and URL Validation Tutorial in PHP Email and URL Validation Tutorial in PHP Fastest Email and URL Validation Method in PHP
You must be logged in to post a comment