Was wanting to know how to check using jquery if a input contains a number higher then 99
8 Answers
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
9 Comments
Naftali
@Lawrence i added on if you are checking it on input change
Naftali
@Lawrence, im guessing thats not for abt 5 mins due to stack restrictions ^_^
Lawrence
Yes that is true it wont let me say its answered for a couple more minutes
Naftali
@Lawrence and now that time restriction should be up ^_^
herostwist
you don't need parsefloat or parseint: ('99' == 99) = true. also: ("100" > 99) = true
|
if ($("#myfield").val() > 99) {
}
1 Comment
herostwist
its nothing to do with jQuery. in JavaScript ('100' == 100) = true
if(parseInt($(YOURINPUT).val()) > 99) // do something
2 Comments
herostwist
you don't need parsefloat or parseint: ('99' == 99) = true. also: ("100" > 99) = true
Damb
@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 :)
If by input you mean text, then use the following:
if (parseFloat($("#inputid").val()) > 99) {
//do something
}
1 Comment
herostwist
you don't need parsefloat or parseint: ('99' == 99) = true. also: ("100" > 99) = true
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
}
}
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 */
}
}
}
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.