Solved

validating fields

Posted on 2004-08-16
7
204 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.

758 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

21 Experts available now in Live!

Get 1:1 Help Now