1

I have an HTML form with a few input-fields, and I want to check that the users enter a number into a text field, instead of a string. I tried this:

if(parseInt(form.width.value) < 0) {
    $("#width").attr('placeholder', 'Please input the desired width of the table.');
}

I have tried with a negative number, a positive number, text, but nothing happends. But whatever I try, the code inside the if statement won't be executed. I simply want to check that the user entered a positive integer.

0

4 Answers 4

2

To check if a value is a number, use isNaN:

if(!isNaN(form.width.value) && parseInt(form.width.value) > 0) {
    // form.width.value is a number bigger than 0
}
Sign up to request clarification or add additional context in comments.

Comments

2

You really will be wanting to set the radix (the second argument) for parseInt as depending on the string/value, it could be interpreted as octet, binary, hex, etc.

So make it to a if (form.width.value && parseInt(form.width.value,10)>0){...}.

Comments

2

A word of caution, DON'T take parseInt for granted. It has been mentioned in answers before but I will elaborate it a bit more:

Here is short but more detailed definition of parseInt:

The parseInt(string, radix) function parses a string and returns an integer.

The radix parameter (as an integer number) is used to specify which numeral system to be used (ex. binary, ternary, octal, decimal, hexadecimal etc).

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)

Only the first number in the string is returned!

If the first character cannot be converted to a number, parseInt() returns NaN. If, however, it can, it will return a number. Sometimes this is not what you expected!

Older browsers will result in parseInt("010") as 8, because of older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0". As of ECMAScript 5, the default is the decimal radix (10).

So in short - make sure you the parameter you are passing is sterilized as base 10 decimal number (I think you don't wanna 12,3 as your width) or even better make sure it is in your condition like:

if ( !isNan(form.with.value) && parseInt(form.width.value, 10) > 0 )

Comments

0

You need to clear the inputted value, otherwise the placeholder won't be displayed.

if (parseInt(form.width.value) < 0) {
  $('#width')
    .attr('placeholder', 'Please input the desired width of the table.')
    .val('');
}

2 Comments

This helped me! I took it for granted that the placeholder text automatically wrote over the value of the input field itself, but of course that is not the case. Thanks a lot!
@SindreMoldeklev you're welcome, but also adjust the actual validation logic to what the other answers show.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.