469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

isNum

I have been trying to get this code to work and the isNum seem to be giving
me a problem and I just cannot figure it out any help would me very much
appreciated!!

Thanks in advance!!!

function SubmitSame() {
var usernew, QPSQTY, PTPS, PSSQTY, PSST, ECQTY, ECT, EXQTY, EXT, INQTY,
INTA, SKQTY, SKT, CSQTY, CST, total, result, result1, result2, result3,
result4, result5, result6, result0;
if (document.getElementById) {
usernew = document.getElementById('usernew');
QPSQTY = document.getElementById('QPSQTY');
PTPS = document.getElementById('PTPS');
PSSQTY = document.getElementById('PSSQTY');
PSST = document.getElementById('PSST');
ECQTY = document.getElementById('ECQTY');
ECT = document.getElementById('ECT');
EXQTY = document.getElementById('EXQTY');
EXT = document.getElementById('EXT');
INQTY = document.getElementById('INQTY');
INTA = document.getElementById('INTA');
SKQTY = document.getElementById('SKQTY');
SKT = document.getElementById('SKT');
CSQTY = document.getElementById('CSQTY')
CST = document.getElementById('CST');
total = document.getElementById('total');
}
else {
usernew = document.usernew;
QPSQTY = usernew.QPSQTY;
PTPS = usernew.PTPS;
PSSQTY = usernew.PSSQTY;
PSST = usernew.PSST;
ECQTY = usernew.ECQTY;
ECT = usernew.ECT;
EXQTY = usernew.EXQTY;
EXT = usernew.EXT;
INQTY = usernew.INQTY;
INTA = usernew.INTA;
SKQTY = usernew.SKQTY;
SKT = usernew.SKT;
CSQTY = usernew.CSQTY;
CST = usernew.CST;
total = usernew.total;
}
if (!isNum(QPSQTY.value)) {
return ; }
}

Jul 23 '05 #1
6 18026
Rick wrote:
I have been trying to get this code to work and the isNum seem to be giving
me a problem and I just cannot figure it out any help would me very much
appreciated!!
[...] }
if (!isNum(QPSQTY.value)) {
return ; }
}


I think you are confused with isNaN(), which returns true if the
argument is not a number.

isNaN(QPSQTY.value)

will return true if the text entered into QPSQTY is not a
number. Here, play with this:

<input type="text" value="6" onblur="
document.getElementById('xx').innerHTML =
this.value + ' isNaN? : ' + isNaN(this.value);
">
<span id="xx"></span>
e.g. 23e345 *is* a valid number and will return false.
.23 is a valid number and will return false.
123qwe is not a valid number and will return true.

--
Fred
Jul 23 '05 #2
Wow that worked perfect.

What is the isNum for?

Thanks you very much!!!
"Fred Oz" <Oz****@iinet.net.auau> wrote in message
news:KD****************@news.optus.net.au...
Rick wrote:
I have been trying to get this code to work and the isNum seem to be
giving
me a problem and I just cannot figure it out any help would me very much
appreciated!!

[...]
}
if (!isNum(QPSQTY.value)) {
return ; }
}


I think you are confused with isNaN(), which returns true if the
argument is not a number.

isNaN(QPSQTY.value)

will return true if the text entered into QPSQTY is not a
number. Here, play with this:

<input type="text" value="6" onblur="
document.getElementById('xx').innerHTML =
this.value + ' isNaN? : ' + isNaN(this.value);
">
<span id="xx"></span>
e.g. 23e345 *is* a valid number and will return false.
.23 is a valid number and will return false.
123qwe is not a valid number and will return true.

--
Fred

Jul 23 '05 #3
Rick wrote:
Wow that worked perfect.

What is the isNum for?
Nothing.

Thanks you very much!!!


You're welcome.

[...]
As a tip, here is a better way of doing what you are doing. It
does two things:

Puts your variables into objects, one for text one for numbers,
and uses the forms collection to avoid using getElementById.

You could include the DynWrite[1] function to provide
getElementById for those browsers that support document.all, but
I think the forms collection is simpler (assuming that you are
using a form...)

The function I've written just changes the style of a form
element depending on whether isNaN returns true or false, hardly
an exhaustive test of whether the input really is a number but
illustrative anyway.

I could have done the isNaN test when reading the values, but
that wouldn't show getting the values back from the object.

1. <URL:http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>Mod Style Play</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<style type="text/css">
..isText {font-weight: normal; color: blue;}
..isNumber {font-weight: bold; color: red;}
</style>
<script type="text/JavaScript">

function SubmitSame(theForm) {
// Just a guess, but are these the names
// of inputs in a form? If so, put them in an array

// all the text elements (just guessing)
var txtIn = {
'usernew':'','QPSQTY':'','PTPS':'','PSSQTY':'','PS ST':'',
'ECQTY':'','ECT':'','EXQTY':''
};
// all the number elements (just guessing)
var numIn = {
'EXT':'','INQTY':'','INTA':'','SKQTY':'','SKT':'',
'CSQTY':'','CST':'','total':''
};
var result, result1, result2, result3,
result4, result5, result6, result0, t, n;

// Now just loop through the elements of the objects and use
// the forms collection - no getElementById/all issues
for ( t in txtIn) {
txtIn[t] = theForm.elements[t].value;
}
for ( n in txtIn) {
txtIn[n] = theForm.elements[n].value;
}

// Test to see if isNaN & modify class accordingly
// Demonstrates looping through elements of an object
for (t in txtIn) {
theForm.elements[t].className =
(isNaN(txtIn[t]))? 'isText':'isNumber';
}
for (n in numIn) {
theForm.elements[n].className =
(isNaN(numIn[n]))? 'isText':'isNumber';
}

}
</script>
</head>
<body>

<form name="formA" action="">
<div>
<input type="text" size="20" name="usernew">usernew</input><br>
<input type="text" size="20" name="QPSQTY">QPSQTY</input><br>
<input type="text" size="20" name="PTPS">PTPS</input><br>
<input type="text" size="20" name="PSSQTY">PSSQTY</input><br>
<input type="text" size="20" name="PSST">PSST</input><br>
<input type="text" size="20" name="ECQTY">ECQTY</input><br>
<input type="text" size="20" name="ECT">ECT</input><br>
<input type="text" size="20" name="EXQTY">EXQTY</input><br>
<input type="text" size="20" name="EXT">EXT</input><br>
<input type="text" size="20" name="INQTY">INQTY</input><br>
<input type="text" size="20" name="INTA">INTA</input><br>
<input type="text" size="20" name="SKQTY">SKQTY</input><br>
<input type="text" size="20" name="SKT">SKT</input><br>
<input type="text" size="20" name="CSQTY">CSQTY</input><br>
<input type="text" size="20" name="CST">CST</input><br>
<input type="text" size="20" name="total">total</input><br>
<input type="reset">
<input type="button" value="stuff"
onclick="SubmitSame(this.form);">
</div>
</form>
<span id="debugText"></span>
<br><br>

<input type="text" value="6" onblur="
document.getElementById('xx').innerHTML =
this.value + ' isNaN? : ' + isNaN(this.value);
">
<span id="xx"></span></body>
</html>

--
Fred

Jul 23 '05 #4
Rick wrote:
Wow that worked perfect.

What is the isNum for?
Nothing.

Thanks you very much!!!


You're welcome.

[...]
As a tip, here is a better way of doing what you are doing. It
does two things:

Puts your variables into objects, one for text one for numbers,
and uses the forms collection to avoid using getElementById.

You could include the DynWrite[1] function to provide
getElementById for those browsers that support document.all, but
I think the forms collection is simpler (assuming that you are
using a form...)

The function I've written just changes the style of a form
element depending on whether isNaN returns true or false, hardly
an exhaustive test of whether the input really is a number but
illustrative anyway.

I could have done the isNaN test when reading the values, but
that wouldn't show getting the values back from the object.

1. <URL:http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>Mod Style Play</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<style type="text/css">
..isText {font-weight: normal; color: blue;}
..isNumber {font-weight: bold; color: red;}
</style>
<script type="text/JavaScript">

function SubmitSame(theForm) {
// Just a guess, but are these the names
// of inputs in a form? If so, put them in an array

// all the text elements (just guessing)
var txtIn = {
'usernew':'','QPSQTY':'','PTPS':'','PSSQTY':'','PS ST':'',
'ECQTY':'','ECT':'','EXQTY':''
};
// all the number elements (just guessing)
var numIn = {
'EXT':'','INQTY':'','INTA':'','SKQTY':'','SKT':'',
'CSQTY':'','CST':'','total':''
};
var result, result1, result2, result3,
result4, result5, result6, result0, t, n;

// Now just loop through the elements of the objects and use
// the forms collection - no getElementById/all issues
for ( t in txtIn) {
txtIn[t] = theForm.elements[t].value;
}
for ( n in numIn) {
numIn[n] = theForm.elements[n].value;
}

// Test to see if isNaN & modify class accordingly
// Demonstrates looping through elements of an object
for (t in txtIn) {
theForm.elements[t].className =
(isNaN(txtIn[t]))? 'isText':'isNumber';
}
for (n in numIn) {
theForm.elements[n].className =
(isNaN(numIn[n]))? 'isText':'isNumber';
}

}
</script>
</head>
<body>

<form name="formA" action="">
<div>
<input type="text" size="20" name="usernew">usernew</input><br>
<input type="text" size="20" name="QPSQTY">QPSQTY</input><br>
<input type="text" size="20" name="PTPS">PTPS</input><br>
<input type="text" size="20" name="PSSQTY">PSSQTY</input><br>
<input type="text" size="20" name="PSST">PSST</input><br>
<input type="text" size="20" name="ECQTY">ECQTY</input><br>
<input type="text" size="20" name="ECT">ECT</input><br>
<input type="text" size="20" name="EXQTY">EXQTY</input><br>
<input type="text" size="20" name="EXT">EXT</input><br>
<input type="text" size="20" name="INQTY">INQTY</input><br>
<input type="text" size="20" name="INTA">INTA</input><br>
<input type="text" size="20" name="SKQTY">SKQTY</input><br>
<input type="text" size="20" name="SKT">SKT</input><br>
<input type="text" size="20" name="CSQTY">CSQTY</input><br>
<input type="text" size="20" name="CST">CST</input><br>
<input type="text" size="20" name="total">total</input><br>
<input type="reset">
<input type="button" value="stuff"
onclick="SubmitSame(this.form);">
</div>
</form>
<span id="debugText"></span>
<br><br>

<input type="text" value="6" onblur="
document.getElementById('xx').innerHTML =
this.value + ' isNaN? : ' + isNaN(this.value);
">
<span id="xx"></span></body>
</html>

--
Fred
Jul 23 '05 #5
*** Rick wrote/escribió (Mon, 21 Mar 2005 04:09:03 GMT):
I have been trying to get this code to work and the isNum seem to be giving
me a problem and I just cannot figure it out any help would me very much
appreciated!!


I can think of this:

function isNum(data){
return typeof(data)=='number';
}

document.writeln(isNum());
document.writeln(isNum(-2));
document.writeln(isNum(0));
document.writeln(isNum(33));
document.writeln(isNum(3.1416));
document.writeln(isNum('951'));
document.writeln(isNum('foo'));
document.writeln(isNum(document));
document.writeln(isNum(this));

Returns:

false
true
true
true
true
false
false
false
false
--
-- Álvaro G. Vicario - Burgos, Spain
-- Don't e-mail me your questions, post them to the group
--
Jul 23 '05 #6
JRS: In article <Vu****************@news.optus.net.au>, dated Mon, 21
Mar 2005 07:13:57, seen in news:comp.lang.javascript, Fred Oz
<Oz****@iinet.net.auau> posted :

You could include the DynWrite[1] function to provide
getElementById for those browsers that support document.all, ... 1. <URL:http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html>

There is a confusion somewhere.

DynWrite is in the FAQ proper, section 4.15 : it does not provide
getElementById, but it provides a means of doing one of the things that
is commonly done with getElementById.

There may be a different implementation of DynWrite in the Notes; but
any function of that name there should have the same parameters and
result.
I now think that it would be well to use inline code to provide
getElementById, and then to have a simple DynWrite using getElementById
(if wanted in more than one or two places; for clarity and compactness).

It's not generally sensible to use isNaN or its logical inverse to
validate numeric input, since in most cases one does not want to accept
all possible representations of all possible IEEE Doubles. Far better
to use RegExps : <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://www.jibbering.com/faq/> JL/RC: FAQ of 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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Dirk Deimeke | last post: by
4 posts views Thread by FatboyCanteen | last post: by
5 posts views Thread by ulyses | last post: by
5 posts views Thread by achraf.b | last post: by
61 posts views Thread by warint | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.