Solved

validating fields

Posted on 2004-08-16
7
205 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now