473,473 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

integer checking ?

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

Similar topics

2
by: Sean Dettrick | last post by:
I am wondering if it is possible to increment an integer at compile time using template<int N> (or by any other method, e.g. compiler directives etc), across multiple source files. This may...
9
by: Dante | last post by:
I'm converting a C# program to VB.net, but i'm having problems converting a integer to a byte in the same way as the c# program does. //C# program int i = 137694; byte b = (byte) i; //b...
61
by: John Baker | last post by:
When declaring an integer, you can specify the size by using int16, int32, or int64, with plain integer being int32. Is integer the accepted default in the programming community? If so, is...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
5
by: Řyvind Isaksen | last post by:
I have a page with an optional integer-field, and one asp:calendar control. I use a stored procedure to save the data in SQL Server. When all fields contains data, the code works great! But if the...
19
by: Robbie Hatley | last post by:
For your enjoyment, a function that expresses any integer with absolute value less-than-or-equal-to nine quintillion in any base from 2 to 36. (For larger bases, you could expand the "digit"...
40
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at:...
4
by: Raymond | last post by:
Source: http://moryton.blogspot.com/2007/08/detecting-overflowunderflow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum...
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
9
by: tsuyois | last post by:
Hi, I just signed in to this excellent network. I hope I could get some answers to many questions I have in writing C compilers. My first question is: Is "integer demotion" required in...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
1
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: 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?
0
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 ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.