javascript - Validate for double type in textbox - Stack Overflow

What is the best way to checking if a valid double type number "##.##" is entered in a text b

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745377297a4625059.html

相关推荐

  • javascript - Validate for double type in textbox - Stack Overflow

    What is the best way to checking if a valid double type number "##.##" is entered in a text b

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信