Module 5 PHP Form Validations
Module 5 PHP Form Validations
Validation Fields
Email Email
preg_match function
It searches string for pattern, returning true if pattern exists, and false otherwise.
Validate Name
$name = $_POST[‘name’];
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
It shows a simple way to check if the name field only contains letters and whitespace. If
the value of the name field is not valid, then store an error message.
Validate Email
$email = $_POST["email"];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
dnnelcaguin 1
Urdaneta City University Elective 102: Server Side Coding
College of Computer Studies Module 5: Form Validations
Validate URL
$website = POST["website"];
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i", $website)) {
$websiteErr = "Invalid URL";
}
It shows a way to check if a URL address syntax is valid (this regular expression also
allows dashes in the URL). If the URL address syntax is not valid, then store an error message:
Validate Number
$age = $_POST[‘age’];
if (!preg_match("/^[0-9]*$/",$age)) {
$ageErr = "Only numbers allowed";
}
It shows a simple way to check if the age field only contains numbers. If the value of the
name field is not valid, then store an error message.
Validate Decimal
$price = $_POST["price"];
if (!filter_var($pricel, FILTER_VALIDATE_FLOAT)) {
$price = “Invalid decimal format";
}
It shows a simple way to check if the price field only contains numbers with decimal. If
the value of the name field is not valid, then store an error message.
dnnelcaguin 2