469,603 Members | 2,049 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

Integer Check

What is the best way to validate a field as a positive or negative
integer?

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #1
9 8926
myvar < 0
myvar > 0 ?
"Dennis M. Marks" <de******@domain.invalid> wrote in message
news:200420040733235484%de******@domain.invalid...
What is the best way to validate a field as a positive or negative
integer?

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

Jul 23 '05 #2
if (isNan(myVariable)) {
alert('incorrect, not a number');
} else {
if ((myVariable % 1)!=0) {
alert('Number can not be a floating-point');
} else return true;

return false;
}
Jul 23 '05 #3
In article <40*********************@news.xs4all.nl>, Vincent van
Beveren <vi*****@provident.remove.this.nl> wrote:
if (isNan(myVariable)) {
alert('incorrect, not a number');
} else {
if ((myVariable % 1)!=0) {
alert('Number can not be a floating-point');
} else return true;

return false;
}


Thank you (except it has to be isNaN)
I would never have thought of your way to check for integer.

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #4
Vincent van Beveren wrote:
if (isNan(myVariable)) {
alert('incorrect, not a number');
} else {
if ((myVariable % 1)!=0) {
alert('Number can not be a floating-point');
} else return true;

return false;
}

Why not:
function isInteger(num){
return (!isNaN(num) && num%1==0);
}
?
Mick
Jul 23 '05 #5
Mick White wrote on 20 apr 2004 in comp.lang.javascript:
Vincent van Beveren wrote:
if (isNan(myVariable)) {
alert('incorrect, not a number');
} else {
if ((myVariable % 1)!=0) {
alert('Number can not be a floating-point');
} else return true;

return false;
}

Why not:
function isInteger(num){
return (!isNaN(num) && num%1==0);
}
?
Mick


return !(isNaN(num) || !(num % 1));

or

return !isNaN(num) && !!(num % 1);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #6
JRS: In article <20*************************@domain.invalid>, seen in
news:comp.lang.javascript, Dennis M. Marks <de******@domain.invalid>
posted at Tue, 20 Apr 2004 07:33:23 :
What is the best way to validate a field as a positive or negative
integer?


How about a zero one? Do you want to allow a floating-point form of a
value which happens to be integer, such as 1e6 or 3.000 ? How about
0xFF ?

OK = /^[+-]?\d+$/.test(field)
tests for optional sign followed by only decimal digits. \d{1,6} might
be preferred, likewise [+- ] .

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7
>>Why not:
function isInteger(num){
return (!isNaN(num) && num%1==0);
}
?
Mick

return !(isNaN(num) || !(num % 1));

or

return !isNaN(num) && !!(num % 1);


I just wrote the code as a draft. Any of these are good.

return (!isNaN(num)) && ((num % 1)==0)

would probably be the most relayable... since num % 1 is a
calculation, it would be better to see if it matches a number,
even though 0 == false.

Jul 23 '05 #8
Vincent van Beveren wrote on 21 apr 2004 in comp.lang.javascript:
return !(isNaN(num) || !(num % 1));

or

return !isNaN(num) && !!(num % 1);


I just wrote the code as a draft. Any of these are good.

return (!isNaN(num)) && ((num % 1)==0)

would probably be the most relayable... since num % 1 is a
calculation, it would be better to see if it matches a number,
even though 0 == false.


I do not know about this relaying,
but any nonzero number is considered true in javascriot.

var nonzeronumber = 2;
alert(!!nonzeronumber) // gives true

var zeronumber = 0;
alert(!!zeronumber) // gives false

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #9
Evertjan. wrote:
Vincent van Beveren wrote on 21 apr 2004 in comp.lang.javascript:
return !(isNaN(num) || !(num % 1));

or

return !isNaN(num) && !!(num % 1);


I just wrote the code as a draft. Any of these are good.

return (!isNaN(num)) && ((num % 1)==0)

would probably be the most relayable... since num % 1 is a
calculation, it would be better to see if it matches a number,
even though 0 == false.


I do not know about this relaying,
but any nonzero number is considered true in javascriot.

var nonzeronumber = 2;
alert(!!nonzeronumber) // gives true

var zeronumber = 0;
alert(!!zeronumber) // gives false


If there is a desire to explicitly return a boolean value, but avoid the
extra comparisons, it might be practical to apply the NOT operator to
the result of the whole expression:-

return !(isNaN(num)||(num % 1));

Richard.
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by FatboyCanteen | last post: by
25 posts views Thread by junky_fellow | last post: by
9 posts views Thread by Chris Botha | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Harry Strybos | last post: by
42 posts views Thread by thomas.mertes | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.