Solved

validating fields

Posted on 2004-08-16
7
209 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
Industry Leaders: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make login page safer 3 32
MySQL Persistent Connections 10 33
How to remove a section of the main menu from Magento 1.9.3 3 28
geting data from the array list 6 12
This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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