Jump to content

[SOLVED] How do I validate a number field in a form?


iconicCreator

Recommended Posts

Okay I have been teaching myself PHP form processing and validation.

 

What I'm trying to do is validate a form field that requires numbers. How do I do this?

I searched google and found some codes but can't understand it.

 

The problem is this, I want the error message to appear in the form label.

Example:

<label for"phoneNumber">Phone Number: ERROR MESSAGE GOES HERE </label>
<input type="text" name="phoneNumber" />

 

How do I add this validation so that this format is required 555-555-555 and an error message appears if the user enters the wrong format or if the user enter more then nine digits.

 

Any ideas or tutorial will be greatly appreciated.

 

IC

I'm assuming you're using POST as the method for your form, and you call the same page to process it.

 

So (I will be foregoing the usual mysql injection checks and so on to concentrate on the real issue):

 

<?php

  $error = '';
  if(array_key_exists('phoneNumber', $_POST))
  {
    if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/', $_POST['phoneNumber']))
    {
      $error = 'Wrong phone number!';
    }
  }

?>

<label for"phoneNumber">Phone Number: <?php echo $error; ?> </label>
<input type="text" name="phoneNumber" />

I'm assuming you're using POST as the method for your form, and you call the same page to process it.

 

So (I will be foregoing the usual mysql injection checks and so on to concentrate on the real issue):

 

<?php

  $error = '';
  if(array_key_exists('phoneNumber', $_POST))
  {
    if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/', $_POST['phoneNumber']))
    {
      $error = 'Wrong phone number!';
    }
  }

?>

<label for"phoneNumber">Phone Number: <?php echo $error; ?> </label>
<input type="text" name="phoneNumber" />

 

Hey thanks for your help! I tried this out and worked with it.

However, the phone number field keeps indicating wrong number even when I typed a number in the form phone number field. Eg: 555-555-555 comes up as in correct number.

 

Any ideas when this is?

 

This is what I have so far.

 

<?php

  $error = '';
  if(array_key_exists('phoneNumber', $_POST))
  {
    if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/', $_POST['phoneNumber']))
    {
      $error = 'Required!';
    }
  }

?>
<div class="phone_fieldWrapper">
<label for="phoneNumber">Phone Number: <span class="warning"> <?php echo $error; ?></span></label>
  <input name="phoneNumber" type="text" class="phoneNumber_inputControl" id="phoneNumber" />
</div>

 

Thanks for your patience with my ignorance!

Be careful with validating phone number fields if users don't all have US phone numbers.

 

Does it accept country codes? e.g. +44(0)161775544

 

If you will only ever have US users then you don't have a problem.

 

That's an excellent point so lets assumed I was going to validate U.S and international phone numbers, the what would I need to add? Eg: 233-243-556941

 

IC

If you want to accept international phone numbers then that means any phone number in the world?

 

If so then you will have to relax the validation I think because lets say a user enters his international phone number e.g here in the UK +44(0)161987654, if your validation fails on that then the user will just enter any garbage that passes the validation and you will have garbage data in your database.

 

The best way to check your reg expression is google for some international phone numbers and see if they pass your validation.

 

What I tend to do when validating phone numbers is use relaxed validation, for example check there are no alphabetic characters present, check there is some data present, check there are no symbols present except ()+-

 

Afterall if the user does not want to tell you their phone number, they will enter a made up one regardless of your validation checks.

If you want to accept international phone numbers then that means any phone number in the world?

 

If so then you will have to relax the validation I think because lets say a user enters his international phone number e.g here in the UK +44(0)161987654, if your validation fails on that then the user will just enter any garbage that passes the validation and you will have garbage data in your database.

 

The best way to check your reg expression is google for some international phone numbers and see if they pass your validation.

 

What I tend to do when validating phone numbers is use relaxed validation, for example check there are no alphabetic characters present, check there is some data present, check there are no symbols present except ()+-

 

Afterall if the user does not want to tell you their phone number, they will enter a made up one regardless of your validation checks.

 

Very great point, I have on occasion enter fake phone numbers when required and I don't want to provide it.

So I think I'm just going make phone numbers U.S only.

and if I was going to make an international phone number required, I'll just relax the validation to insure no alphabetic characters.

 

But how do I go about doing this? Lets say the numbers start with a 0 and I have between zero and nine.

 

IC

with out or with country code.

 

example country code

+44(0)161-775-544

 

normal code

161-775-544

 

Example corrected.

<?php

  $error = '';
  if(array_key_exists('phoneNumber', $_POST))
  {
    if(!preg_match('/^((\+)[0-9]{2}\([0-9]\)[0-9]{3}-|[0-9]{3}-)[0-9]{3}-[0-9]{3}$/', $phonenumber))
    {
      $error = 'Required!';
    }
  }

?>
<div class="phone_fieldWrapper">
<label for="phoneNumber">Phone Number: <span class="warning"> <?php echo $error; ?></span></label>
  <input name="phoneNumber" type="text" class="phoneNumber_inputControl" id="phoneNumber" />
</div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.