Form Validation

<!DOCTYPE html>
		<title>Form Validation</title>
		<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
			<input type="text" name="name" placeholder="Name" /><br />
			<input type="email" name="email" placeholder="E-mail" /><br />
			<input type="tel" name="ph" placeholder="Phone Number" /><br />
			<input type="url" name="website" placeholder="Website" /><br />
			<input type="submit" name="submit" value="Submit" />
			if(isset($_POST['submit'])) {
				/* Checking length of the string */
				if(strlen($_POST['name']) > 3) {

					/* Matching name with regular expression */
					if(!preg_match("/^[a-zA-Z ]*$/",$_POST['name']))
						echo "<br />Only letters and white space allowed";
					echo "<br />Invalid name!";
				if(strlen($_POST['email']) != 0) {

					/* Validate email using 'filter_var()' */
					if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
						echo "<br />Invalid email format!";
					echo "<br />E-mail can't be empty!";

				if(strlen($_POST['ph']) != 0) {

					/* 'ctype_digit()' checking numeric characters */
					if(strlen($_POST['ph']) < 10 || !ctype_digit($_POST['ph'])) 
						echo "<br />Invalid phone number!";
					echo "<br />Phone number can't be empty!";
				if(strlen($_POST['website']) != 0) {

					/* Matching URL with regular expression */
					if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $_POST['website'])) 
						echo "<br />Invalid web URL!";
					echo "<br />Web URL can't be empty!";

/* Output */
Name = Geek24
E-mail = [email protected]
Phone = 984521
Website =

Only letters and white space allowed
Invalid email format!
Invalid phone number!!
Invalid web URL!

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!