
Originally Posted by
clueful
isNAN will return false for "" because an empty string evaluates to null which equals 0.
Yes.

Originally Posted by
clueful
The easiest solution is to use a regex to test for the presence of a digit, then call isNaN if necessary.
Code:
if ( !document.formPizza.txtBoxQuantity.value.match(/\d/) || isNaN(document.formPizza.txtBoxQuantity.value))
Easiest? To evaluate if a form's element's value is a number or not (as much as I love regular expressions) it would be easier to:
Code:
if(value == '' || isNaN(value)) rejection code goes here

Originally Posted by
clueful
The onclick handler must return the return value of the validator, otherwise the latter has no effect:
Code:
onclick="return validatePizzaForm()"
Forms should generally be validated on the form element's onsubmit event, not the onclick event of a button.
Further, form elements should be accessed as a part of the elements collection of the form as part of the forms collection of the document:
Code:
document.forms['formName'].elements['elementName']
Not:
Code:
document.formName.elementName
This can be greatly simplified if the form itself is passed to the script code. For example, one can:
HTML Code:
<form action="#" onsubmit="return validate(this);">
Then in the validate function:
Code:
function validate(f){
f = f.elements;
. . .
};
In a case like the above:
Code:
document.formPizza.txtBoxQuantity.value
Which should be:
Code:
document.forms['formPizza']['txtBoxQuantity'].value
would become:
Code:
f['txtBoxQuantity'].value
and would still be being referenced properly as a part of the document.forms['formName'].elements collections.
Bookmarks