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

javascript - Detect if number is INT or FLOAT in jquery - Stack Overflow

programmeradmin6浏览0评论

I am currently taking input in a text-box and on-submit, I am validating the input, the input needs to be a number greater than 0, it should be integer and not contain decimal point. The problem is I am not able to find any method by which we can detect it is integer or contains decimal point. Here is the code I am using.

    txt[5] = $(this).parents('p').find('input:text').val();
    if (txt[5] == "" || isNaN(txt[5]) || txt[5] <= 0) 
    {
        alert("Incorrect Quantity");
        return false;
    }

Basically as you see I am validating the input given by user, I want to show them a message if the input is wrong, so I am currently using txt[5]%1!=0 to make sure only INT is allowed, but I am open to new answers.

I am currently taking input in a text-box and on-submit, I am validating the input, the input needs to be a number greater than 0, it should be integer and not contain decimal point. The problem is I am not able to find any method by which we can detect it is integer or contains decimal point. Here is the code I am using.

    txt[5] = $(this).parents('p').find('input:text').val();
    if (txt[5] == "" || isNaN(txt[5]) || txt[5] <= 0) 
    {
        alert("Incorrect Quantity");
        return false;
    }

Basically as you see I am validating the input given by user, I want to show them a message if the input is wrong, so I am currently using txt[5]%1!=0 to make sure only INT is allowed, but I am open to new answers.

Share Improve this question edited Oct 20, 2013 at 19:27 Ankur asked Oct 20, 2013 at 19:10 AnkurAnkur 751 gold badge2 silver badges9 bronze badges 4
  • 3 Why not simply use parseInt()? It truncates floats and 'unparseable' input will return NaN. – Kiruse Commented Oct 20, 2013 at 19:12
  • possible duplicate of How to check if a number is float or integer? – Felix Kling Commented Oct 20, 2013 at 19:13
  • @FelixKling: Bear in mind that the answer to that dupe will not work if the supplied argument is a string, because typeof "5" is string. – Matt Ellen Commented Oct 20, 2013 at 19:20
  • @MattEllen: I don't think it's very difficult to adjust the solution. – Felix Kling Commented Oct 20, 2013 at 19:39
Add a ment  | 

3 Answers 3

Reset to default 4

The simplest way is to parse the text as a Number, coerce to integer, and see if they are equal:

var numbertxt5 = Number(txt[5]);
if(numbertxt5 | 0 == numbertxt5) { /* int */
} else if (!isNaN(numbertxt5)) { /* float */
} else { /* not a number */
txt[5] = parseInt($(this).parents('p').find('input:text').val(), 10);

This won't validate the number (see Nirk's answer for that), but if it's not an int, it will be converted to one.

That is because JavaScript has only one numerical type called number. It is neither int nor float, but it behaves like both at a time. However, you do have two methods that you can use for this parseInt and parseFloat.

var result = parseInt($(this).parents('p').find('input:text').val());
txt[5] = result;

Using parseFloat is similar. Eg.

var result = parseFloat($(this).parents('p').find('input:text').val());
txt[5] = result;

The second example is not what you want but it is here just for your reference.

UPDATE: @bfavaretto made a good point with using radix which I omitted in parseInt. Have a look at this page for reference http://www.w3schools./jsref/jsref_parseint.asp

发布评论

评论列表(0)

  1. 暂无评论