In: Computer Science
Write an HTML document to provide a form that collects family names and telephone
numbers. The family name should start with a capital letter, followed by at least one
lowercase letter, and not include any spaces or any other characters, and the length
should not be more than 30 characters. The phone numbers must be in the format
(ddd)-ddd-dddd. Write a PHP script that checks the submitted last name and the
telephone number to be sure that they conform to the required format and then returns
a response indicating whether the format was correct. Use regular expressions to
validate the names and phone numbers.
I use preg_match() function in PHP for regular expression and the syntax I use explain in the code.
syntax
preg_match("regular expression",string)
Html code
PHP code
$l1=$_POST['l1'];
$p1=$_POST['p1'];
echo $l1;
echo $p1;
//^[A-Z] is for 1st character capital
//[-a-z] is for 2nd character in small letter
//([a-zA-Z]) from 3rd character to 30th character any letter in small or caps
if(!preg_match("/^[A-Z][-a-z]([a-zA-Z]){1,30}$/",$l1))
echo "Lastname Error";
// ^ starting $ is for ending
//^\( is for 1st character is first bracket (
//[0-9]{3} is for next 3 characters will be from 0-9
//- is a character
// \) is for close bracket
// [0-9]{3} is for next 3 characters will be from 0-9
// [0-9]{4} is for next 4 characters will be from 0-9
if(!preg_match("/^\([0-9]{3}\)-[0-9]{3}-[0-9]{4}$/",$p1))
echo "
Phone Error";
?>