Let's learn about form validation using PHP. This tutorial will help you to learn how to perform validation on the form on the client side. Client-side validation saves the round trip time to the server, as they are performed on the client's computer and before sending it to the server for final processing.
In this tutorial, I have used a simple registration form that user fills while registration. When a user submits this form, I validate the user data and show appropriate error messages to the user.
Here, I have used preg_match() and quantifiers.
preg_match(pattern, string)
This function searches pattern in the string. If it finds then it returns true else false.
PHP Code segment formValidation.php
<?php
$erroruname="";
$errorpwd="";
$un="";
$pwd="";
$errorname="";
$erroraddr="";
$erroremail="";
$errorage="";
$errormobile="";
if(isset($_POST['btnSubmit']))
{
if ($_POST['txtUname']== null)
{
$erroruname = "please enter user name";
}
else if (preg_match('/[\s-]/',$_POST['txtUname']))
{
$erroruname = "White space and dashes not allowed";
}
else if (!preg_match("/^[a-zA-Z0-9]*$/",$_POST["txtUname"]))
{
$erroruname = "Username can contains only letters and numbers";
}
else
{
$un = $_POST['txtUname'];
echo "User Name is : ".$un."<br/>";
}
if (empty($_POST['txtPwd']))
{
$errorpwd = "please enter password";
}
else if (strlen($_POST['txtPwd'])<6)
{
$errorpwd = "Please enter at least 6 digit password";
}
else
{
$pwd = $_POST['txtPwd'];
echo "Password is : ".$pwd;
}
if (empty($_POST['txtFname']))
{
$errorname = "Please enter full name";
} else if (preg_match("/[^a-zA-Z ]/",$_POST['txtFname']))
{
$errorname = "Please enter letters a-z and A-Z only!";
}
else
{
$name = $_POST['txtFname'];
}
if (empty($_POST['txtAge']))
{
$errorage = "Please enter age";
} else if (preg_match("/\D/",$_POST['txtAge']))
{
$errorage = "Please enter numbers only for Age";
}
else
{
$age = $_POST['txtAge'];
}
$email = htmlspecialchars($_POST['txtEmail']);
if(empty($_POST['txtEmail']))
{
$erroremail = "Please enter email id";
}else if (!preg_match("/[@]/",$email)) //else if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$erroremail = "Please enter valid email id";
}
if(empty($_POST['txtMobile']))
{
$errormobile = "Please enter mobile no.";
} else if((strlen($_POST['txtMobile'])<10) || (strlen($_POST['txtMobile'])>10) || (preg_match("/\D/",$_POST["txtMobile"])))
{
$errormobile = "Please enter 10 digits";
}
if(empty($_POST['addr']))
{
$erroraddr = "Please enter address";
}
}
?>
HTML Code segment formValidation.php
<html>
<head><title>Login</title></head>
<body bgcolor="aqua"><center>
<h1> User Registration Form </h1>
<form name="frmLogin" id="frmLogin" method="POST" action="<?php $PHP_SELF ?>"
style="padding:50px 300px 40px; border:2px dashed gray;width:200px;font-family:arial;font-size:17;" >
Enter User Name:<br/> <input type="text" name="txtUname" />
<span style="color:red;"><?php echo $erroruname; ?></span><br /><br />
Enter Password : <br/><input type="password" name="txtPwd" />
<span style="color:red;"><?php echo $errorpwd; ?></span><br/><br />
Enter Full Name:<br/> <input type="text" name="txtFname" />
<span style="color:red;"><?php echo $errorname; ?></span><br /><br />
Enter Address : <br/><textarea name = "addr" rows="3" cols="22"></textarea>
<span style="color:red;"><?php echo $erroraddr; ?></span><br/><br />
Enter Email:<br/> <input type="text" name="txtEmail" />
<span style="color:red;"><?php echo $erroremail; ?></span><br /><br />
Enter Age : <br/><input type="text" name="txtAge" />
<span style="color:red;"><?php echo $errorage; ?></span><br/><br />
Enter Mobile : <br/><input type="text" name="txtMobile" />
<span style="color:red;"><?php echo $errormobile; ?></span><br /><br/><br/>
<input type="submit" value="Login" name="btnSubmit" />
</form>
</center>
</body>
</html>
Conclusion:
In conclusion, Validation is very important part of form submission. So learning about form validation using PHP will help you to create secure and user-friendly websites.
By implementing robust validation techniques, developers ensure the integrity and accuracy of user-submitted data, guarding against potential security vulnerabilities and enhancing the overall user experience.
Always analyse your form and decide what sort of validation you need to perform to save round trip time to server. By implementing robust validation techniques, you can ensure the integrity and accuracy of data submitted by the user, protect against potential security loopholes and enhance the overall user experience.