3

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.

Community
  • 1
  • 1
Vangel Tzo
  • 8,885
  • 3
  • 26
  • 32

4 Answers4

12
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.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
  • This evaluates to `true` on floats, so unless OP is willing to chop up products for customers that want 1.5 qty... – MrCode Dec 12 '12 at 11:55
  • @MrCode - He said "positive numeric numbers". Nowhere did he specify that he needed an integer. Perhaps he is selling something by weight, he didn't specify. – Levi Botelho Dec 12 '12 at 11:58
  • In any case, you could always add something like `number % 1 == 0;` – Cerbrus Dec 12 '12 at 12:02
  • If that is what he wants. He is going to have to say so if that is the case, however. – Levi Botelho Dec 12 '12 at 12:04
2

you can use regexp
your pattern is ^\d+$.

^ - starting of the line
$ - end of the line
\d - digit
+ - 1 or more times

if(mystr.match(/^\d+$/))
{
    //
}

if you need the first symbol not to be 0 you should write /^(0|[^1-9]\d*)/
read regexp documentation to understand this pattern.

shift66
  • 11,760
  • 13
  • 50
  • 83
0

Try with search() function and RegExp. Like this:

if (form.qty.value.search(/^([0-9\.]+)$/)==-1)
{
alert ("Please enter the field");
document.forms[0].qty.focus()
document.forms[0].qty.select()
return false;
}
CoursesWeb
  • 4,179
  • 3
  • 21
  • 27
0

Check the following answer: https://stackoverflow.com/a/10270826/783743

In your case I would do the following:

function validate(form) {
    return isPositive(+form.qty.value);
}

function isPositive(n) {
    return +n === n && n >= 0;
}

Explanation:

  1. The expression +form.qty.value coerces the string to a number. If the string is empty or is not a number it will express NaN.
  2. The function isPositive only returns positive if n is a number (+n === n) and it's greater than or equal to zero (n >= 0).
Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • This does not appear to work for float values. Also, I'd suggest explaining how your bitwise operations work (Like the `+` when passing the parameter. – Cerbrus Dec 12 '12 at 11:55
  • @Cerbrus - The plus operator (`+`) is not a bitwise operator. It's used to coerce a primitive value (boolean, string or number) to a number. The function `isPositive` expects `n` to be a number. The expression `+n === n` only returns `true` if `n` is a number and it's not `NaN`. – Aadit M Shah Dec 12 '12 at 12:07