Solved

validating fields

Posted on 2004-08-16
7
211 Views
Last Modified: 2006-11-17
Hi,
   I need to validate certain fields....like telephone numbers which should not take string value....just numbers. I tried to work out with is_Integer but it didnt work so I was wondering if there are other ways for it. Thanks for any help.

Sid
0
Comment
Question by:Sid123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11811803
you need to use is_numeric for that

http://www.php.net/manual/en/function.is-numeric.php
0
 
LVL 3

Expert Comment

by:alskdj80
ID: 11811944
hmm, the problem with using is_numeric is that it wont accept hyphens (in phone numbers)... perhaps you might have to use preg_match

ie...
if (!preg_match("/[^0-9\-\(\)\ ]+$/", $myphonefield)) {
  //error
}

this will allow -, (, ), a space, and numbers... in case someone types in (123) 123-1234 or something like that

of course you can even make it more strict, but its better to make it more lenient because you may frustrate users if they are forced to conform to your restrictions...

look at http://us3.php.net/preg_match for syntax... and look at http://www.regular-expressions.info/tutorial.html for regex info
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11811987
>> the problem with using is_numeric is that it wont accept hyphens

true, but "should not take string value....just number" so it depends on the format of the input

telephone numbers can be just numeric without any additional characters so if this is the case then is_numeric will be fine

incidently your pattern looks as though it may accept just -, (, ) and spaces without numbers?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
alskdj80 earned 50 total points
ID: 11812270
ah yes, i missed the "just number" part of the question, my mistake

hmm, yes, it should be preg_match("/^[0-9\-\(\)\ ]+$/", $myphonefield)
0
 
LVL 4

Expert Comment

by:Skonen
ID: 11812282

$stripped_phone = str_replace(Array('-', ' ', '(', ')'), "", $phonefield);

if (!is_numeric($stripped_phone)){
    //Error
} else {
   //Valid
}

You may also want to do some further rules such as checking the length using strlen() to verify it's a 10 digit number, etc.

Good luck,
    Stuart Konen
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 11840267
Also you can strip all, leaving only the numerics and hyphens.

// not tested...
$num = preg_replace('/[^0-9-]/','',$num);

or stip all but numerics:

$num = preg_replace('/[^0-9]/','',$num);
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11840288
I think the "just numbers" part of the question suggests that the is_numeric function will be sufficient.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question