User Name Validation Code: Use below code, to check if the name field only contains alphabet and white space.
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only alphabet and white space allowed";
}
E-mail Validation Code in PHP
PHP's filter_var() function is used to check whether an email address is in correct format or not.
In the code below, if the e-mail address is not correct, then it gives an error message.
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
Validate URL in PHP: Use below regular expression, to check whether a URL address is valid or not. If the URL address is not valid, then it gives an error message:
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
0 Comment(s)