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

javascript - Validate for double type in textbox - Stack Overflow

programmeradmin0浏览0评论

What is the best way to checking if a valid double type number "##.##" is entered in a text box? so if there is a decimal entered once, don't let users enter it again.

Would regular expressions be the best way to do this?

Any help is much appreciated

What is the best way to checking if a valid double type number "##.##" is entered in a text box? so if there is a decimal entered once, don't let users enter it again.

Would regular expressions be the best way to do this?

Any help is much appreciated

Share Improve this question asked Nov 1, 2012 at 16:48 user793468user793468 4,97624 gold badges84 silver badges126 bronze badges 1
  • 1 regular expressions would be the best – peroija Commented Nov 1, 2012 at 16:49
Add a ment  | 

3 Answers 3

Reset to default 2

You can use regex like this

> "222.22".match(/^\d+.?\d*$/)
["222.22"]

> "222.22.2".match(/^\d+.?\d*$/)
null

You can also just try to convert it to Number e.g.

> isNaN(Number("22.2.2"))
true

Only problem or advantage with checking by converting to Number is that it will allow number like 1e6

> isNaN(Number("1e6"))
false

If you are talking in terms of jQuery, there are many options out there. Most of them involves a regular expression check.

Matching against a valid number is easy enough with regular expressions, if your main concern is preventing further character insertions, read on.

All you need to do is the following.

  1. You need a character map of integers to 'allow' the inputs.
  2. Prevent all other inputs except keys that match the character map.
  3. Further prevent the 'dot' insertion if there is already one.

I will assume you allow patterns that starts with a dot, as long as javascript parses it as if there is a leading zero, and many developers treat this as a short-hand.

Also with the jQuery tag, assumed that you are working with jQuery.

$("#Foo").keydown(function(e) {
    var c = e.keyCode
      , value = $(this).val();

    // Prevent insertion if the inserting character is
    // 1. a 'dot' but there is already one in the text box, or
    // 2. not numerics.
    if ( (c == 190 && value.indexOf('.') > -1) || c < 48 || c > 57 ) {
        e.preventDefault();
        return;
    }
});

If using HTML5 is an option, you can use a new input type called "pattern".

It works like this:

<input id="numberField" type="text" title="You need to provide a number in the form of 2 integer and 2 decimal digits" pattern="\d{2}\.\d{2}" />

Then, you can register an event to make the element uneditable when it loses focus, like this (using jQuery):

$("#numberField").blur(function() { $(this).attr("disabled", "disabled"); });

Alternatively, if you just want to check if what the field contains is a number (any form), you can just check it like this:

$("#numberField").blur(function() {
    if (!isNaN(parseInt($(this).val()))) {
        $(this).attr("disabled", "disabled");
    };
}​);​

Check it out on fiddle.

发布评论

评论列表(0)

  1. 暂无评论