admin管理员组

文章数量:1425753

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);
    }
});

本文标签: jquerySet a max value to a text box in javascriptStack Overflow