473,381 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

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 14557
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.