Chi Shen Chi Shen - 2 months ago 13
PHP Question

contain 12 digit for phone number in user input in php form

I want to set the landLineTelNo is an integer and must contain 12 digit...
here is my php form code....

// Start the session

<!DOCTYPE html>
<html lang="en">
<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>


//define variable and set to empty value

$forenameErr = $surnameErr = $emailErr = $postalAddressErr = $landLineTelNoErr = $mobileTelNoErr = $sendMethodErr = "";
$valid = true;

// if forename is null , make it null , else test_input()
$forename = empty($_POST["forename"]) ? NULL : test_input($_POST["forename"]);

// if surname is null , make it null , else test_input()
$surname = empty($_POST["surname"]) ? NULL : test_input($_POST["surname"]);

// if postalAddress is null , make it null , else test_input()
$postalAddress = empty($_POST["postalAddress"]) ? NULL : test_input($_POST["postalAddress"]);

// if landLineTelNo is null , make it null , else test_input()
$landLineTelNo = empty($_POST["landLineTelNo"]) ? NULL : test_input($_POST["landLineTelNo"]);

// if mobileTelNo is null , make it null , else test_input()
$mobileTelNo = empty($_POST["mobileTelNo"]) ? NULL : test_input($_POST["mobileTelNo"]);

$email = empty($_POST["email"]) ? NULL : test_input($_POST["email"]);

// if sendMethod is null , make it null , else test_input()
$sendMethod = empty($_POST["sendMethod"]) ? NULL : test_input($_POST["sendMethod"]);

if (isset($_POST["submit"])){

//check forename
if($forename === NULL) {
//forename is empty
$forenameErr = "Forename is required";
$valid = false;

} else {
//check characters
if (!preg_match("/^[a-zA-Z ]*$/",$forename)) {
$forenameErr = "Only letters and white space allowed";
$valid = false;

//check surname
if($surname === NULL){
//surname is empty
$surnameErr = "Surname is required";
$valid = false; //false

} else {
//check charaters
if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
$surnameErr = "Only letters and white space allowed";
$valid = false;

//check address
if (!preg_match("/^[a-zA-Z0-9\-\\,. ]*$/", $postalAddress)) {
// check characters
$postalAddressErr = "Invalid Postal Address";
$valid = false;//false

// check if invalid telephone number added
if (!preg_match("/^$|^[0-9]{12}$/",$landLineTelNo)) {
//check number
$landLineTelNoErr = "Invalid telephone number entered";
$valid = false;//false

//check valid mobiel tel no
if (!preg_match("/^$|^[0-9]{11}$/",$mobileTelNo)) {
//check number
$mobileTelNoErr = "Invalid telephone number entered";
$valid = false;//false

//check valid email

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
$valid = false;//false

//check sendMethod
if($sendMethod === NULL){
//send method is empty
$sendMethodErr = "Contact method is required";
$valid = false; //false
} else {
$sendMethod = test_input($_POST["sendMethod"]);

//if valid then redirect

$_SESSION['forename'] = $forename;
$_SESSION['surname'] = $surname;
$_SESSION['email'] = $email;
$_SESSION['postalAddress'] = $postalAddress;
$_SESSION['landLineTelNo'] = $landLineTelNo;
$_SESSION['mobileTelNo'] = $mobileTelNo;
$_SESSION['sendMethod'] = $sendMethod;

header('Location: userdetail.php');

} else{
//user did not submit form!

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;


<div id="wrapper">

<h1>Welcome to Chollerton Tearoom! </h1>

<li><a href="index.html">Home</a></li>
<li><a href="findoutmore.html">Find out more</a></li>
<li><a href="offer.html">Offer</a></li>
<li><a href="credit.html">Credit</a></li>
<li><a href="#">Admin</a></li>
<li><a href="wireframe.html">WireFrame</a></li>

<form id = "userdetail" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

<fieldset id="aboutyou">
<legend id="legendauto">user information</legend>

<label for="forename">Forename: </label>
<input type="text" name="forename" id="forename" value="<?php echo $forename;?>">
<span class="error">* <?php echo $forenameErr;?></span>

<label for="surname">Surname:</label>
<input type="text" name="surname" id="surname" value="<?php echo $surname;?>">
<span class="error">* <?php echo $surnameErr;?></span>

<label for="postalAddress">Postal Address:</label>
<input type="text" name="postalAddress" id="postalAddress" value="<?php echo $postalAddress;?>">
<span class="error"> <?php echo $postalAddressErr;?></span>

<label for="landLineTelNo">Landline Telephone Number:</label>
<input type="text" name="landLineTelNo" id="landLineTelNo" value="<?php echo $landLineTelNo;?>" >
<span class="error"> <?php echo $landLineTelNoErr;?></span>

<label for="mobileTelNo">Moblie:</label>
<input type="text" name="mobileTelNo" id="mobileTelNo" value="<?php echo $mobileTelNo;?>" >
<span class="error"> <?php echo $mobileTelNoErr;?></span>

<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="<?php echo $email;?>">
<span class="error"> </span> <?php echo $emailErr;?> </span>

<fieldset id="future">
<legend>Lastest news</legend>

Choose the method you recommanded to recevive the lastest information
<input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="email") echo "checked";?> value="email">
<input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="post") echo "checked";?> value="post">
<input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="SMS") echo "checked";?> value="SMS">
<span class="error">* <?php echo $sendMethodErr;?></span>

<p><span class="error">* required field.</span></p>

<input type="checkbox" name="checkbox" value="check" id="agree" />
I have read and agree to the Terms and Conditions and Privacy Policy

<input type="submit" name="submit" value="submit" >





here is my redodata.php code that get the data...



$forename = $_SESSION['forename'];
$surname = $_SESSION['surname'];
$email = $_SESSION['email'];
$postalAddress = $_SESSION['postalAddress'];
$landLineTelNo = $_SESSION['landLineTelNo'];
$mobileTelNo = $_SESSION['mobileTelNo'];
$sendMethod = $_SESSION['sendMethod'];

echo "<h1>Successfull submission :</h1>";
echo "<p>Forename : $forename <p/>";
echo "<p>Surname : $surname <p/>";

{echo "postalAddress:NULL";}
else {echo "<p>PostalAddress : $postalAddress </p>";}

{echo "email:NULL";}
else {echo "<p>email : $email</p>";}

{echo "landLineTelNo:NULL";}
else {echo "<p>landLineTelNo : $landLineTelNo </p>";}

{echo "mobileTelNo:NULL";}
else {echo "<p>mobileTelNo : $mobileTelNo </p>";}

echo "<p>sendMethod : $sendMethod </p>";


I've tried "/^[0-9]{12}$/" but it cannot submit the form when it is empty....
it suppose to submit when it is empty as well....
when i tried "/^[0-9]*$/" , everything works....
so how can i set the landLineTelNo to 12 digit or else it cannot submit the form...
it can submit the form if it is empty....


The following regex checks for an empty value or your original regex.


Hope this helps.