Jump to content

Numerical validation


KJ

Recommended Posts

I have a mail form up and running, but I cannot get my telephone input box to only allow numbers.

 

It's probably an easy fix, so if anyone has ideas on how to set the input to numerical only I'd very much appreciate the help.

 

Thanx!!!!

Link to comment
Share on other sites

The precise code will depend on the structure of your PHP code, (assuming you are using PHP), but the basic code will be like this:-

if(preg_match('/[^][0-9]/' , $phone))
{echo "<p>Please only use numbers 0 to 9 inclusive.</p>";}

 

where the [^] means NOT

 

There is also a javascript method.

Edited by Wickham
Link to comment
Share on other sites

One other way to do it with PHP: use the is_numeric() function (http://php.net/manual/en/function.is-numeric.php). This is a bit simpler than using preg_match() and a regular expression, but lacks a bit of the flexibility that regular expressions give you (for example, you could use preg_match() to specify phone numbers in a specific format, such as 000-000-0000 or similar.)

Link to comment
Share on other sites

The precise code will depend on the structure of your PHP code, (assuming you are using PHP), but the basic code will be like this:-

if(preg_match('/[^][0-9]/' , $phone))
{echo "<p>Please only use numbers 0 to 9 inclusive.</p>";}

 

where the [^] means NOT

 

There is also a javascript method.

 

Thanks for the advise Wickham.

I modified the suggested code to suit my structure, which works fine when only letters are input into the 'Telephone' input box, resulting in an error message. However if numbers and letters are input the mail form is passed.

My code is as follows;

 

if (!ereg("^[0-9]" , $_POST['phone'])) {

exit("<p>Sorry!! Invalid telephone format, please use only numbers 0 to 9 inclusive.</p>");

}

Link to comment
Share on other sites

If you aren't going to do it the Falkencreative way, then I can only say that my code worked in one of my pages!

 

ereg and eregi are now deprecated and will be removed from PHP 6 so it's best to use preg_match.

 

You ereg code looks odd to me as !ereg is a negative and ^ is a negative so perhaps you have a double negative but I think falkencreative better give an answer as my PHP isn't very good.

Link to comment
Share on other sites

If you aren't going to do it the Falkencreative way, then I can only say that my code worked in one of my pages!

 

ereg and eregi are now deprecated and will be removed from PHP 6 so it's best to use preg_match.

 

You ereg code looks odd to me as !ereg is a negative and ^ is a negative so perhaps you have a double negative but I think falkencreative better give an answer as my PHP isn't very good.

 

Thanks Wickham!!!

I took on board your comments and modified my code to use the preg_match method as follows;

 

if (preg_match('/[^][0-9]/' , ($_POST['phone']))) {

exit ("<p>Sorry!! Invalid telephone format, please use only numbers 0 to 9 inclusive - no spaces.</p>");

}

 

Worked a treat, I had to add the 'no spaces' as the code appears to require continuous numbers with no breaks.

Is that how it worked when you tried it?

Link to comment
Share on other sites

Thanks Wickham!!!

I took on board your comments and modified my code to use the preg_match method as follows;

 

if (preg_match('/[^][0-9]/' , ($_POST['phone']))) {

exit ("<p>Sorry!! Invalid telephone format, please use only numbers 0 to 9 inclusive - no spaces.</p>");

}

 

Worked a treat, I had to add the 'no spaces' as the code appears to require continuous numbers with no breaks.

Is that how it worked when you tried it?

 

*********************************

 

Following my most recent modification of the 'Telephone' code, I received complaints from our staff that they preferred to read phone numbers in the traditional format with breaks.

We also received a couple of calls from potential clients that found they could not add extensions to their telephone number when using my mail form.

So I modified the code as;

 

if (preg_match('/[^][0-9|| Eext:;]/' , ($_POST['phone']))) {

exit ("<p>Sorry!! Invalid telephone format, please use only numbers 0 to 9 inclusive.<br>Prefix extensions with ex or ext.</p>");

}

 

I hope I can now draw a line under this particular problem, though I have noticed another potential problem with my ageing mail form, which I will attempt to rectify over the coming days, or I may post as a new topic should I need help again, as my PHP skills are quite basic.

 

I'd also like to send a personal thank you to Wickham and Falkencreative for their help and advise.

Link to comment
Share on other sites

Following my most recent modification of the 'Telephone' code, I received complaints from our staff that they preferred to read phone numbers in the traditional format with breaks. We also received a couple of calls from potential clients that found they could not add extensions to their telephone number when using my mail form.

That is one potential problem if you make users enter fields in a certain format. In case you aren't doing this now, you might consider adding some small description text that indicates the format expected "000 000 0000 x0" or whatever, and make sure that if the user enters information in the wrong format, the form validation error also clearly states the correct format.

 

I'm glad to hear that you were able to easily modify the regular expression to allow for extensions. Flexibility is definitely one advantage of regular expressions.

Link to comment
Share on other sites

Can someone please explain how the PHP code for the part in blue in the earlier post works?

It was || Eext:;

part of

if (preg_match('/[^][0-9|| Eext:;]/' , ($_POST['phone']))) {
}

 

The part that I use

if (preg_match('/[^][0-9]/' , ($_POST['phone']))) {
}

I understand, but what does the || do and how does Eext:; relate to either ex or Ext ?

 

At a guess, it's excluding any of the letters Eext in any order but why does it have || before and :; afterwards?

 

I suppose the safe but longer way to get phone numbers is to have three input boxes, one for the area code, one for the number and one for any extension (which can be allowed to be empty) and possibly another for country code. Then if in a database it is simple to sort on area code. It's also easier then to feed from a database and display with a space between the sections of the number.

Edited by Wickham
Link to comment
Share on other sites

In response to the question of why I added the

|| Eext:;

to my code in the following string;

 

if (preg_match('/[^][0-9|| Eext:;]/' , ($_POST['phone']))) {}

 

I wanted to ensure that only numbers '0-9' OR '||' the letters ' Eext' could be input to the Telephone input box.

The space was to allow breaks between numbers, the uppercase E was incase the user input Ext and I included ':;' incase the user input Ext: or ext;

 

I probably made it too complicated and may take Falconcreative's advise and amend my error message to show a format preference.

Or the advise to add another box for the extension, which may well be best.

 

Another potential problem is that the user could repeatedly input the permitted non-numbers, in any order.

 

Thank you!!!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...