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

integer checking ?

P: n/a
mhk
hi,

is there any function in Javascript to check/validate integer value.
there is parseInt(val) but it only checks first digit.

example : to find error in 123k44

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
mhk wrote:
hi,

is there any function in Javascript to check/validate integer value.
there is parseInt(val) but it only checks first digit.

example : to find error in 123k44


myVar = "123k44"

if (myVar == +myVar)

lightly tested.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

P: n/a
mhk


Randy Webb wrote:
mhk wrote:
hi,

is there any function in Javascript to check/validate integer value.
there is parseInt(val) but it only checks first digit.

example : to find error in 123k44


myVar = "123k44"

if (myVar == +myVar)

lightly tested.

******************************
yes ... looks like its working but what that "+" does ?
Thanks

Jul 23 '05 #3

P: n/a
Randy Webb wrote:
[...]
is there any function in Javascript to check/validate integer value.
there is parseInt(val) but it only checks first digit.

example : to find error in 123k44


myVar = "123k44"

if (myVar == +myVar)


But that will "validate" any number, not just integers.

If the requirement is that the variable only contains digits (that is,
no decimal point or any other character), if:

var isInt = /^\d+$/.test(a)

then isInt will be true only if 'a' contains only digits.

However, 0.2e3 is a valid int (it is equivalent to 200) - is that OK
with the OP? And 5.0 is a valid int too, but will fail the test.

Have a read here:

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

and here:

<URL:http://www.jibbering.com/faq/faq_notes/type_convert.html#tcUserIn>

--
Rob
Jul 23 '05 #4

P: n/a
mhk wrote on 16 jan 2005 in comp.lang.javascript:
hi,

is there any function in Javascript to check/validate integer value.
there is parseInt(val) but it only checks first digit. example : to find error in 123k44

<script>
if (isInteger('123k44'))
alert('yes: 123k44')
else
alert('no: 123k44')

if (isInteger('123e44'))
alert('yes: 123e44')
else
alert('no: 123e44')

if (isInteger('123.44'))
alert('yes: 123.44')
else
alert('no: 123.44')
function isInteger(n) {
return (!isNaN(n)) && (Math.floor(n)==n)
}

</script>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #5

P: n/a
Evertjan. wrote:
<snip>
function isInteger(n) {
return (!isNaN(n)) && (Math.floor(n)==n)
}

<snip>

As NaN is specified as never equalling NaN and Math.floor(NaN) results
in NaN:-

function isInteger(n){
return (Math.floor(n) == n);
}

- would be simpler.

Richard.
Jul 23 '05 #6

P: n/a
JRS: In article <cs*******************@news.demon.co.uk>, dated Sun, 16
Jan 2005 16:21:35, seen in news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted :
Evertjan. wrote:
<snip>
function isInteger(n) {
return (!isNaN(n)) && (Math.floor(n)==n)
}

<snip>

As NaN is specified as never equalling NaN and Math.floor(NaN) results
in NaN:-

function isInteger(n){
return (Math.floor(n) == n);
}

- would be simpler.


isInteger("100000000000000000000000000000000000000 0000000.3")
isInteger("9007199254740993")
isInteger("3e+333")
isInteger("3e-333")
isInteger("0xbead")

give true.
Before testing for integer, one should first consider what one really
should be testing for; usually, one wants a digit in [1-9] followed by
not too many decimal digits. The OP should use a RegExp.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Jul 23 '05 #7

P: n/a
function isAllDigitsint(argvalue) {
argvalue = argvalue.toString();
var validChars = "0123456789";
var startFrom = 0;
if (argvalue.substring(0, 2) == "0x") {
validChars = "0123456789abcdefABCDEF";
startFrom = 2;
} else if (argvalue.charAt(0) == "0") {
validChars = "01234567";
startFrom = 1;
} else if (argvalue.charAt(0) == "-") {
startFrom = 1;
}

for (var n = startFrom; n < argvalue.length; n++) {
if (validChars.indexOf(argvalue.substring(n, n+1))
== -1) return false;
}
return true;

checks to see if all digits are int
parseInt this and u should get ur result
Problem ???
this accepts octal.
and 09 fails .. need to improve the script.
Please help

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.