jquery - Set a max value to a text box in javascript - Stack Overflow

I have a text box in the form which should allow only numeric values that is less than the boundary val

I have a text box in the form which should allow only numeric values that is less than the boundary value 2147483647 (max value of Int32)? Can anybody help me in doing this with JQuery? I have tried using by adding a validate plugin in eclipse but it is not working. Thanks in advance.

I have a text box in the form which should allow only numeric values that is less than the boundary value 2147483647 (max value of Int32)? Can anybody help me in doing this with JQuery? I have tried using by adding a validate plugin in eclipse but it is not working. Thanks in advance.

Share Improve this question edited May 16, 2013 at 4:00 Sparky 98.8k26 gold badges202 silver badges290 bronze badges asked May 15, 2013 at 21:45 atlpeteratlpeter 191 silver badge3 bronze badges 4
  • 4 can you show us code? something you tried? We can help you, don't solve the problem for you. – steo Commented May 15, 2013 at 21:47
  • 2 You should read this answer posted on another question. – Patartics Milán Commented May 15, 2013 at 21:50
  • Do you want only positive integers less than 2147483647? – kennebec Commented May 15, 2013 at 21:59
  • did any answer helped you?! – Muhammad Bekette Commented May 19, 2013 at 18:45
Add a ment  | 

3 Answers 3

Reset to default 2

here sample code:

$('#textboxID').on('change',function(){
    if($(this).val()>=2147483647){
    //put error span with nice css
    }
    });

HTML:

<input type="text" id="numberField"  />
<input id="submit" type="submit" value="Submit"  />

JQuery:

$('#submit').click(function(){
    var numberField = $('#numberField');
    var number = parseInt(numberField.val(), 10);
    if(isNaN(number) || number > 2147483647){
        numberField.val('');
        alert('Not a number');
    }
    else
        alert('Number is: '+ number);
});

jsFiddle http://jsfiddle/R3Rx2/1/

You could do something like this:

DEMO: http://jsfiddle/mSSYT/1/

$('#test').on('keyup', function (e) {
    var $self = $(this),
        v = $self.val(),
        max = 2147483647;

    //blank any input that isint a number
    if (!/^\d*$/.test(v)) {
        $self.val('');
        return;
    }

    //trim the value until it meets the condition
    if (v >= max) { 
        while (v >= max) {
            v = v.substring(0, v.length - 1);
        }

        $self.val(v);
    }
});

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

相关推荐

  • jquery - Set a max value to a text box in javascript - Stack Overflow

    I have a text box in the form which should allow only numeric values that is less than the boundary val

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信