Velocity Reviews > Checking whether a string is all digits

Checking whether a string is all digits

Christopher Benson-Manica
Guest
Posts: n/a

 06-06-2005
I'm trying to check whether a string is all digits. This part is
easy:

function allDigits( str ) {
var foo=str.split( '' ); // better than charAt()?
for( var idx=0; idx < foo.length; idx++ ) {
if( !isDigit(foo[idx]) ) {
return false;
}
}
return true;
}

I'm not sure about how to implement isDigit(). Is this the best way?

function isDigit( s ) {
if( s.length > 1 ) {
return false;
}
var nums='1234567890';
return nums.indexOf(s) != -1;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

drWot
Guest
Posts: n/a

 06-06-2005
String.prototype.isdigits=function(){
return (/\D/.test(this)==false);
}

This returns true if there are no non-digits in the string.
Since it is a prototype method, call it for a string str like this:

if(str.isdigits()){do something}
else {do something else}

Lasse Reichstein Nielsen
Guest
Posts: n/a

 06-06-2005
Christopher Benson-Manica <(E-Mail Removed)> writes:

> I'm trying to check whether a string is all digits.

For that, regular expressions is the simplest way, by some orders
of magnitude

function allDigits(str) {
return /^\d*\$/.test(str); // consists of only digits from start to end
}

or even:

function allDigits(str) {
return !/\D/.test(str); // doesn't contain non-digit
}

This accepts the empty string, which is, technically, all digits
(there is nothing but digits). If you want only non-empty strings,
change it to:

function allDigits(str) {
return /^\d+\$/.test(str);
}

Good luck.
/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Vic Sowers
Guest
Posts: n/a

 06-06-2005

"Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
news:d828a0\$s84\$(E-Mail Removed)...
> I'm trying to check whether a string is all digits. This part is
> easy:
>
> function allDigits( str ) {
> var foo=str.split( '' ); // better than charAt()?
> for( var idx=0; idx < foo.length; idx++ ) {
> if( !isDigit(foo[idx]) ) {
> return false;
> }
> }
> return true;
> }
>
> I'm not sure about how to implement isDigit(). Is this the best way?
>
> function isDigit( s ) {
> if( s.length > 1 ) {
> return false;
> }
> var nums='1234567890';
> return nums.indexOf(s) != -1;
> }
>
> --
> Christopher Benson-Manica | I *should* know what I'm talking about - if I
> ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

allDigits = str.split(/\d/).length==0

Lasse Reichstein Nielsen
Guest
Posts: n/a

 06-06-2005
"Vic Sowers" <Mail@Vic_NOSPAM_Sowers.com> writes:

> "Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
> news:d828a0\$s84\$(E-Mail Removed)...
>> I'm trying to check whether a string is all digits.

....
> allDigits = str.split(/\d/).length==0

Either do:

var allDigits = (str.split(/\d/).length == (str.length+1));

or

var allDigits = (str.split(/\D/).length <= 1);

The length of someString.split(...) is never 0.
/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

cosmic fool
Guest
Posts: n/a

 06-06-2005
you guys are nerds

Zif
Guest
Posts: n/a

 06-07-2005
cosmic fool wrote:
> you guys are nerds
>
>

Read the FAQ. Do not top post. Quote what you are replying to.

<URL:http://jibbering.com/faq/#FAQ2_3>

--
Zif

cosmic fool
Guest
Posts: n/a

 06-07-2005
exactly

"Zif" <(E-Mail Removed)> wrote in message
newsw7pe.1946\$(E-Mail Removed)...
> cosmic fool wrote:
> > you guys are nerds
> >
> >

>
> Read the FAQ. Do not top post. Quote what you are replying to.
>
> <URL:http://jibbering.com/faq/#FAQ2_3>
>
> --
> Zif

Vic Sowers
Guest
Posts: n/a

 06-07-2005

"Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Vic Sowers" <Mail@Vic_NOSPAM_Sowers.com> writes:
>
>> "Christopher Benson-Manica" <(E-Mail Removed)> wrote in
>> message
>> news:d828a0\$s84\$(E-Mail Removed)...
>>> I'm trying to check whether a string is all digits.

> ...
>> allDigits = str.split(/\d/).length==0

>
>
> Either do:
>
> var allDigits = (str.split(/\d/).length == (str.length+1));
>
> or
>
> var allDigits = (str.split(/\D/).length <= 1);
>
> The length of someString.split(...) is never 0.

Odd... "1234567890".split(/\d/).length is 0 in IE and 11 in Firefox.

RobG
Guest
Posts: n/a

 06-07-2005
Vic Sowers wrote:
[...]
>>Either do:
>>
>> var allDigits = (str.split(/\d/).length == (str.length+1));
>>
>>or
>>
>> var allDigits = (str.split(/\D/).length <= 1);
>>
>>The length of someString.split(...) is never 0.

>
>
> Odd... "1234567890".split(/\d/).length is 0 in IE and 11 in Firefox.
>
>

You think that's weird? Try this:

Firefox reports: 1 then 2, but IE reports 1 then 0. Explain that and
keep a straight face.

Using a letters rather than a digits gives exactly the same result.

Firefox: 1 then 2, IE: 1 then 0.

Here's another:

y = [ ]; alert(y.length) // Both browsers say 0
y = [ ,,, ]; alert(y.length) // Firefox says 3, IE says 4
y = [ ,,,'' ]; alert(y.length) // Firefox says 4, IE says 4

If the last element is undefined, Firefox doesn't add it to the array.

One or the other has got to be in error. Bottom line: be very careful
when building arrays.

--
Rob