Test if field value is numeric and not empty

html, javascript

Solution

if (Number(form.qty.value) > 0) {
    // Only executes if value is a positive number.
}

Note that if the value is an empty string the statement will not return true. The same goes for `null`, `NaN` (obviously) and `undefined`. String representations of numbers are transformed into their numeric counterparts.

Problem

Possible Duplicate: Validate that a string is a positive integer So I want to check a form field if it's empty and to allow it to has only positive numeric numbers, no spaces or letters. So far I succeed the first part to check if the field is empty with the following code: ``` function validate(form) { var elements = form.elements; if (form.qty.value == "") { alert("Please enter the field"); document.forms[0].qty.focus() document.forms[0].qty.select() return false; } return true; } ``` Somewhere in the form: `<form action="addtocart.php" method ="post" onsubmit="return validate(this);" />` ... But now I want to allow only numbers as well as to check if the field is empty. I have found some scripts but I don't know how to merge the code into one valid script.

Original source

Related problems