By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

Check String for Digits ?

P: n/a
Hi guys,

I want to validate a string to make sure it contains only digits.

0001 = true
6669 = true
00a0 = false
6.665 = false

What's the best way to go about doing that ?

Take care,
Cyrus
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Cyrus D. wrote:
Hi guys,

I want to validate a string to make sure it contains only digits.

0001 = true
6669 = true
00a0 = false
6.665 = false

What's the best way to go about doing that ?


Lots of ways to do it.
This is one way: preg_match('/^\d{4,4}$/', $var)

/Bent
Jul 17 '05 #2

P: n/a
Cyrus D. wrote:
Hi guys,

I want to validate a string to make sure it contains only digits.

0001 = true
6669 = true
00a0 = false
6.665 = false

What's the best way to go about doing that ?

Take care,
Cyrus


preg_match('`^[0-9]+$`',$string);

returns TRUE if *only* digits are in the string, no matter what length
it is.

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #3

P: n/a
Cyrus D. wrote:
I want to validate a string to make sure it contains only digits. [snip] What's the best way to go about doing that ?

I'd use a regular expression:

php$ cat onlydigits.php
<?php
function only_digits($x) {
return !preg_match('/[^0-9]/', $x);
}

if (only_digits('0001')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('6669')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('00a0')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('6.665')) echo 'OK'; else echo 'BAD'; echo "\n";
?>

php$ php onlydigits.php
OK
OK
BAD
BAD

Check the manual @
http://www.php.net/preg_match

--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html
Jul 17 '05 #4

P: n/a
Cyrus D. wrote:
Hi guys,

I want to validate a string to make sure it contains only digits.

0001 = true
6669 = true
00a0 = false
6.665 = false

What's the best way to go about doing that ?


An alternative suggestion without regexps:

is_numeric($string) && strval(floor(intval($string))) == $string

Checks if $string is numeric; and then removes possible decimal places and
compares that to the original (to make sure there are no points).

--
Markus
Jul 17 '05 #5

P: n/a
On Wed, 20 Oct 2004, Markus Ernst wrote:
Cyrus D. wrote:
I want to validate a string to make sure it contains only digits.


An alternative suggestion without regexps:

is_numeric($string) && strval(floor(intval($string))) == $string

Checks if $string is numeric; and then removes possible decimal places and
compares that to the original (to make sure there are no points).


Technically this will also allow strings which begin with "-" or "+", and
those which contain a "." followed solely by zeros. This might be what is
required, though.

--
Matt
Jul 17 '05 #6

P: n/a
Matt Raines wrote:
On Wed, 20 Oct 2004, Markus Ernst wrote:
Cyrus D. wrote:
I want to validate a string to make sure it contains only digits.


An alternative suggestion without regexps:

is_numeric($string) && strval(floor(intval($string))) == $string

Checks if $string is numeric; and then removes possible decimal
places and compares that to the original (to make sure there are no
points).


Technically this will also allow strings which begin with "-" or "+",
and those which contain a "." followed solely by zeros. This might be
what is required, though.


Well it is rather a mistake in my thinking... a corrected version:

is_numeric($string) && number_format(abs($string), 0, "", "") === $string

--
Markus
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.