2

I need this field to only allow numbers to be input to the field and there must be 11 digits that have been input into the "Mobile Number" field, this is the form I have created

<form name="myForm" id="userDetails" action="formProcess.php" onSubmit="return validateForm()" method="post">
  <fieldset class="custInf">
    <h3> User Details </h3>

    <label class="inputArea" for="fName">Forename :</label>
    <input type="text" name="forename" id="fname" placeholder="Enter First Name" maxlength="20" size="15"></input>

    <label class="inputArea" for="sName">Surname :</label>
    <input type="text" name="surname" id="surname" placeholder="Enter Last Name" maxlength="20" size="15"></input>

    <label class="inputArea" for="email">Email :</label>
    <input type="email" name="email" id="email" placeholder="Enter Email Address" maxlength="40" size="15" /></input>

    <label class="inputArea" for="hmph">Landline number :</label>
    <input type="tel" name="landLineTelNo" id="hmphone" placeholder="Enter Landline no." maxlength="11" size="15"></input>

    <label class="inputArea" for=" mobileTelNo">Mobile number :</label>
    <input type="tel" name=" mobileTelNo" id="mobile" placeholder="Enter Mobile no." maxlength="11" size="15"></input>

    <label class="inputArea" for="address">Postal Address :</label>
    <input type="text" name="postalAddress" id="address" placeholder="Enter House no." maxlength="25" size="15"></input>
  </fieldset>

  <fieldset class="contactType">
    <h3>
	How would you like to be contacted?
	</h3>
    <label class="radioBt" for="sms">SMS</label>
    <input id="smsBut" type="radio" name="sendMethod" value="SMS"></input>

    <label class="radioBt" for="email">Email</label>
    <input type="radio" name="sendMethod" value="Email"></input>

    <label class="radioBt" for="post">Post</label>
    <input type="radio" name="sendMethod" value="Post"></input>
  </fieldset>

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

  <input class="submitBut" type="submit" name="submitBut" value="Submit" </input>

Here is the PHP I have created so far to validate the user input `//Ensures user enters information required

$fname = $_REQUEST['forename'];
if (empty($fname)) {
die("<p>Enter a first name</p>\n");
}

$surname = $_REQUEST['surname'];
if (empty($surname)) {
die("<p>You must enter a surname</p>\n");
}

$email = $_REQUEST['email'];
if (empty($email)) {
die("<p>You need to enter an email</p>\n");
}

$hmphone = isset($_REQUEST['hmph']) ? $_REQUEST['hmph'] : null ;

$mobile = $_REQUEST['mobileTelNo'];
if (empty($mobile)) {
die("<p>Enter a Mobile Number</p>\n");
}

$address = $_REQUEST['postalAddress'];
if (empty($address)) {
die("<p>Enter your postal address</p>\n");
}

$sendMethod = $_REQUEST['sendMethod'];
if (empty($sendMethod)) {
die("<p>Please choose a contact option</p>\n");
}

$check = $_REQUEST['check'];
if (empty($check)) {
die("<p>Please select the Terms and Conditions to continue</p>\n");
}`
5
  • javascript is the usual way to validate forms Commented Apr 19, 2016 at 15:59
  • "there must be 11 digits" I guess you never want to work with countries outside of North America? Commented Apr 19, 2016 at 16:02
  • 3
    You have to, no, make that always validate server-side. @Mihai Commented Apr 19, 2016 at 16:02
  • 1
    Use html 5 patern w3schools.com/tags/att_input_pattern.asp and maxlenght w3schools.com/tags/att_input_maxlength.asp and then you can validate again with js functions and php functions Commented Apr 19, 2016 at 16:04
  • @JorgeMejia The second example there allows characters other than numbers and less than 11 characters. However I still agree with Jay Blanchard that you must validate server-side. Commented Apr 19, 2016 at 16:08

3 Answers 3

1

You can pattern match inside the form by adding the following code inside the relevant input field thanks to the html5 pattern tag. More details on the pattern tag here: http://www.w3schools.com/tags/att_input_pattern.asp

pattern="[0-9]{11}"

It's quite similar on the server side php checking thankfully!

$isValid = preg_match("/[0-9]{11}/", $formvalue);

$isValid will return true (1) if it matches or false (0) if it doesn't match.

preg_match is a very useful function in general for custom validation. There's more details on it available here: http://php.net/manual/en/function.preg-match.php

Sign up to request clarification or add additional context in comments.

Comments

1

If you're sure your rule is correct you can do this:

if (empty($mobile) || preg_match('/^[0-9]{11}$/', $mobile) != 1 ) {
    die("<p>Enter a Mobile Number</p>\n");
}

from the docs:

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

The regular expression explained:

  • ^ assert position at start of the string
  • [0-9] match a single character present in the list below
  • Quantifier: {11} Exactly 11 times
  • 0-9 a single character in the range between 0 and 9
  • $ assert position at end of the string

13 Comments

Even though it's not what OP asked for, I would do something like $mobile = preg_replace('/[^\d]/', '', $_POST['mobile']); and just allow them to enter it any way they want. So if they do +1 (123) 555-5555 it will still match correctly. Just make sure there are total 11 digits somewhere in the string.
This one has worked thanks mate:) I havnt checked others yet but this one does the job
I agree @Mike, I would tend to be more 'forgiving'.
Actually on applying the code, everytime i go to submit the form i keep getting the mobile error message... any suggestions?
I applied an edit or two @OliverQueen, did you get them all?
|
0

you can validate with this:

if(empty($number)) {
    echo '<p> Please enter a value</p>';
} else if(!is_numeric($number)) {
    echo '<p> Data entered was not numeric</p>';
} else if(strlen($number) != 11) {
    echo '<p> The number entered was not 6 digits long</p>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.