...

View Full Version : NaN and defining variables



id123
06-07-2004, 04:49 PM
hi i want to check a form field for being non-numeric.
i have this currently
function Validate(theForm) {
var missing = "";
if (isNaN(theform.quantity.value)) missing += "\n - Quantity must be a number\n";
if (theForm.quantity.value == "") missing += "\n - Please enter a value for quantity\n";
if (theForm.quantity.value > 2000) missing += "\n - Quantity must be below 2000 for online quotations. Please call 0800876876876 for a quote\n";

if (missing != "") {
alert("The following errors occurred\n"
+ missing
+ "\nPlease complete and resubmit");
return false;
} else return true;


}


the nan isnt being picked up. is this the correct way to reference to a form value using the NaN function

thanks

Willy Duitt
06-07-2004, 05:11 PM
Your form quantity value is a string. You must convert that to a number which you can do by multiplying by 1.

if (isNaN(theform.quantity.value*1))

requestcode
06-07-2004, 05:20 PM
If no entry is made in the form element I don't believe that isNaN will pick that up. You might try using regular expressions to validate. Here is a link to a tutorial on the subject:
http://www.javascriptkit.com/javatutors/re.shtml

another one:
http://www.webreference.com/js/column5/index.html

Willy Duitt
06-07-2004, 05:24 PM
Ooops;

I didn't read the entire code, only the first line which dealt with the isNaN question, but RequestCode is correct in that if the field is empty, both conditions will be met (not a number and empty). You can try changing that line to:


if (!theform.quantity.value=='' && isNaN(theform.quantity.value*1))

id123
06-07-2004, 05:37 PM
i have tried this
if (!theform.quantity.value=='' && isNaN(theform.quantity.value*1)) missing += "\n - Quantity must be a number\n";

and it is bypassing this and not showing any error message???

any ideas?

thanks

Willy Duitt
06-07-2004, 05:46 PM
What do you mean it doesn't work? It works for me....


<script>
function test(theform){
var missing = '';
if (!theform.quantity.value=='' && isNaN(theform.quantity.value*1)) missing += "\n - Quantity must be a number\n";
alert(missing)
}
</script>

<form>
<input type="text" name="quantity" onchange="test(this.form)">
</form>

Willy Duitt
06-07-2004, 05:50 PM
Check your cases! You are using both theform & theForm

glenngv
06-08-2004, 04:33 AM
This is quite confusing

if (!theform.quantity.value=='' && ...

Why not just

if (theform.quantity.value!='' && ...

?

and you don't need to multiply it with 1.

if (theform.quantity.value!='' && isNaN(theform.quantity.value)) {
missing += "\n - Quantity must be a number\n";
alert(missing)
}

If quantity is required, then you can make the condition like this:

if (theform.quantity.value=='' || isNaN(theform.quantity.value)) {
missing += "\n - Quantity must be a number\n";
alert(missing)
}

Willy Duitt
06-08-2004, 08:22 AM
This is quite confusing

if (!theform.quantity.value=='' && ...



you find not equal to confusing? :confused: :eek:

glenngv
06-08-2004, 08:47 AM
Yes, but because of the == and not of the ! operator. When you see ==, you tend to overlooked the ! at the start because that's not common. IMO, this is the common way and more simplified version and direct to the point.

if (theform.quantity.value!='' &&

Isn't that better?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum