473,663 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ 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 12576
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('0 001')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('6 669')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('0 0a0')) 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($str ing) && strval(floor(in tval($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($str ing) && strval(floor(in tval($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($str ing) && strval(floor(in tval($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($str ing) && number_format(a bs($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
3973
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 data that is in this field can vary. It's a list of mechanical equipment, and how it is designated varies based on how the customer has them labeled. For example, a list of their equipment might look like: CH-1 CH-2 CH-3
9
19690
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
13986
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]) and the index is the location after "." in name. First, i should check the given name is a valid float-point, and then, to check the number in name in index locaion is > 0 or not? eg:
10
60210
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 no other characteres allowed: wrong: 12345ABV wrong: 12#$as1123
6
7607
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, Karthi
4
12716
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 Thanks for ur inputs Anoop
10
2733
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
6002
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. Example: invoice number = 700123 invoice check number 700123(checknumber) that check number should make the entire invoice number able to validate using mod10/luhn
5
3619
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 <fstream> using namespace std; string removeAllWhite( string eric) {
0
8437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4185
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.