<?php
// variables preset
$nameval = $ageval = $emailval = $urlval = "";
$name = $age = $email = $url = "";
// check submitted method
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// check NAME (letters)
if (empty($_POST["name"]))
{
$nameval = "Name is required";
}
else
{
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z]*$/", $name)) // validating string
{
$nameval = "Only letters allowed";
}
}
// check AGE (number)
if (empty($_POST["age"]))
{
$ageval = "Age is required";
}
else
{
$age = test_input($_POST["age"]);
if (!preg_match("/^[0-9][0-9]*$/", $age)) // validating numbers
{
$ageval = "Only numbers allowed";
}
}
// check EMAIL (email format)
if (empty($_POST["email"]))
{
$emailval = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // validating email
{
$ageval = "Only emails allowed";
}
}
// check WEBSITE (url format)
if (empty($_POST["url"]))
{
$urlval = "URL is required";
}
else
{
$url = test_input($_POST["url"]);
if (!preg_match("/\b(?:https?|ftp):\/\/|www\.[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) // validating URL
{
$urlval = "Only proper URL allowed";
}
}
}
?>


Comments
No comments have been made yet.
Please login to leave a comment. Login now