6

Was wanting to know how to check using jquery if a input contains a number higher then 99

8 Answers 8

24

try this:

if(parseInt($("selector").val()) > 99) {
    /*if it is*/
}

or if you are checking it on change:

$("selector").change(function(){
     if(parseInt(this.value) > 99){
        /*if it is*/
     } 
})

Edit as said in the below comments, it might be better to use parseFloat instead of parseInt to compare the numbers

Sign up to request clarification or add additional context in comments.

9 Comments

@Lawrence i added on if you are checking it on input change
@Lawrence, im guessing thats not for abt 5 mins due to stack restrictions ^_^
Yes that is true it wont let me say its answered for a couple more minutes
@Lawrence and now that time restriction should be up ^_^
you don't need parsefloat or parseint: ('99' == 99) = true. also: ("100" > 99) = true
|
3
if ($("#myfield").val() > 99) {

}

1 Comment

its nothing to do with jQuery. in JavaScript ('100' == 100) = true
1
if(parseInt($(YOURINPUT).val()) > 99) // do something

2 Comments

you don't need parsefloat or parseint: ('99' == 99) = true. also: ("100" > 99) = true
@herostwist: I already got into situation, where the conversion didn't work correctly without parseInt, so I like doing it safe since then. I don't remember the context, so don't ask :)
1

If by input you mean text, then use the following:

if (parseFloat($("#inputid").val()) > 99) {
   //do something
}

1 Comment

you don't need parsefloat or parseint: ('99' == 99) = true. also: ("100" > 99) = true
1

Simply use this:

if ($("input.INPUTNAME").val() > 99) {
//above 99 code
}

You may want to have an else command, if the input is below 99.

if ($("input.INPUTNAME").val() > 99) {
    //above 99 code.
    }
else{
     //Not above 99 code.
}

Comments

0

This would do it

var value = $("input selector").eq(0).val();
if(isNaN(value)){
 //do stuff
}

else{
var num = value - 0;
if(num>99)
{
 //value is greater than 99
}

}

2 Comments

what is the num = value - 0; for?
This is to convert a string representation of a number into a true number. this will convert "99" string literal to 99 number. There are various ways of doing this (parseInt, parseFloat etc.). This is one way that I use.
-1

Use this:

html:

<input type="text" id="myInput" />

jquery:

var nMyInput = $("#myInput").val();
if (typeof nMyInput != "undefined"){
    if(!isNaN(nMyInput)){
        if (parseInt(nMyInput) > 99) {
            /* do your stuff */
        }
    }
}

2 Comments

wow you dont really need all those if statements beforehand. parseInt should do all that for you
I didn't knew the parseInt did all the job (detect if are undefined and if the text are not number) ^_^'
-1

I guess typically with jQuery you would have to have the name of the form

<form id="myForm" action="comment.php" method="post"> 
    number: <input id="form_number" type="text" name="number" /> 
    Comment: <textarea name="comment"></textarea> 
    <input type="submit" value="Submit Comment" /> 
</form>

you would select the form and its input.

var myInt = parseInt(jQuery("#form-number-url").attr("value"));

then you can compare it with any other integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.