473,466 Members | 4,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Check String for Digits ?

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
6 12557
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
9
by: Henke | last post by:
Hi! Is there a simple way to check if a string entered in a text box is a valid integer? Thanks in advance! /Henke
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
10
by: nescio | last post by:
hello, i must check if the value of a text field is a number (they have to fill in a number between 7 and 10 digits), so: 1234567 is allowed and 123456789999 is not allowed; also, there are...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
by: Anoop | last post by:
Hi All I am getting two different outputs when i do an operation using string.digits and test.isdigit(). Is there any difference between the two. I have given the sample program and the output ...
10
by: PythonistL | last post by:
I am a newbie with Javascript and would like to check if a text provided by a users , during filling in an input field , has digits only. Any idea? Thank you L
3
by: Kris | last post by:
I need to create a valid mod10 or luhn number that can be attached to an invoice, I have found several examples showing how to validate a credit card number or validate a number using mod10/luhn. ...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.