最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript parseInt-error - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Dec 21, 2015 at 18:50 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jan 6, 2015 at 14:01 Sindre MoldeklevSindre Moldeklev 231 silver badge8 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 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
}

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){...}.

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 )

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('');
}
发布评论

评论列表(0)

  1. 暂无评论