This basic PHP script can be used for form validations for multiple field types. It has inbuilt support for email validation using filters, basic name validation, and URL validations. You can customize this script according to your own requirements.
<?php
$name="";
$email="";
$url="";
$age="";
$tos="";
$nameError="";
$emailError="";
$urlError="";
$ageError="";
$tosError="";
if($_SERVER['REQUEST_METHOD']=='POST'){
/*-- Name Validation Starts--*/
if(empty($_POST['name'])){
$nameError="Please Enter Your Name";
}
else {
$name=checkFormData($_POST['name']);
if(!preg_match("/^[a-zA-Z-' ]*$/",$name)){
$nameError = "Please Enter a Valid Name";
$name="";
}
}
/*-- Name Validation Ends--*/
/*-- Email Validation Starts --*/
if(empty($_POST['email'])){
$emailError="Please Enter Your Email";
}
else{
$email=checkFormData($_POST['email']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = "Invalid email format";
$email="";
}
}
/*-- Email Validation Ends --*/
/*-- URL Validation Starts --*/
if(empty($_POST['url'])){
$urlError="Please Enter Your Website";
}
else{
$url=checkFormData($_POST['url']);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) {
$urlError = "Invalid URL format";
$url="";
}
}
/*-- URL Validation Ends --*/
/*-- Radio Button Validation Starts --*/
if(empty($_POST['age'])){
$ageError="Please Confirm Your Age";
}
else{
$age=checkFormData($_POST['age']);
}
/*-- Radio Button Validation Ends --*/
/*-- Checkbox Validation Starts --*/
if(empty($_POST['tos'])){
$tosError="Please agree to our terms and conditions";
}
else{
$tos=checkFormData($_POST['tos']);
}
/*-- Checkbox Validation Ends --*/
}
function checkFormData($fieldVal){
$fieldVal = trim($fieldVal);
$fieldVal = stripslashes($fieldVal);
$fieldVal = htmlspecialchars($fieldVal);
return $fieldVal;
}
?>
<html>
<body>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<input type="text" name="name" placeholder="Your Name" value="<?php echo $name; ?>">
<span style="color:red"><?php echo $nameError;?></span><br><br>
<input type="text" name="email" placeholder="Your Email" value="<?php echo $email; ?>">
<span style="color:red"><?php echo $emailError;?></span><br><br>
<input type="text" name="url" placeholder="Your Website" value="<?php echo $url; ?>">
<span style="color:red"><?php echo $urlError;?></span><br><br>
<span>Age:</span>
<input type="radio" name="age" value="Above 18" <?php if (isset($age) && $age=="Above 18") echo "checked";?>>
<span>Above 18</span>
<input type="radio" name="age" value="Below 18" <?php if (isset($age) && $age=="Below 18") echo "checked";?>>
<span>Below 18</span>
<span style="color:red"><?php echo $ageError;?></span><br><br>
<input type="checkbox" name="tos" value="confirm" <?php if (isset($tos) && $tos=="confirm") echo "checked";?>>
<span>I agree to the terms and conditions</span>
<span style="color:red"><?php echo $tosError;?></span><br><br>
<input type="submit" value="Submit Form">
</form>
</body>
</html>
Need Professional Support for Your Website Problems?
Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.
Hire Us