Solved

validating fields

Posted on 2004-08-16
7
207 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

821 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