Jquery input text allow decimal and value between 0.00 and 100
HTML :
<input name="score type="number" min="0.01" max="100" step="0.01">
is showing invalid when i enter "1111" but i want to validation after i click the submit button
<input type="text" name="score">
<input type="submit">
Script:
if($(this).val()<= 0.01 && ($(this).val() <=100)){ // allow 0.01 to 100 100
return true;
}
i want to allow only decimal in the Field
Jquery input text allow decimal and value between 0.00 and 100
HTML :
<input name="score type="number" min="0.01" max="100" step="0.01">
is showing invalid when i enter "1111" but i want to validation after i click the submit button
<input type="text" name="score">
<input type="submit">
Script:
if($(this).val()<= 0.01 && ($(this).val() <=100)){ // allow 0.01 to 100 100
return true;
}
i want to allow only decimal in the Field
Share Improve this question edited Mar 28, 2017 at 15:09 Mohammad Fareed asked Mar 28, 2017 at 15:00 Mohammad FareedMohammad Fareed 1,9826 gold badges30 silver badges61 bronze badges 5- 2 Is there a question here? – Sablefoste Commented Mar 28, 2017 at 15:00
- 1 and your answer is. use html5 and not javascript for that look @ Marijn's answer – mtizziani Commented Mar 28, 2017 at 15:02
- input type="number" is not working properly when i change the value of that field – Mohammad Fareed Commented Mar 28, 2017 at 15:03
- @Developer: and how are you changing the value of that field? What does "not working properly" mean? – David Thomas Commented Mar 28, 2017 at 15:04
- Do you want the checking to be done when the user submits the information (on click of a button) or when the user types in the text ( on keypress ) ? – lhavCoder Commented Mar 28, 2017 at 15:06
2 Answers
Reset to default 8You can use HTML for this:
<input type="number" min="0" max="100" step="0.01">
If you don't want to use HTML:
$(document).on("ready", function() {
$("#form").submit(function(e) {
var scoreVal = $("#score").val();
if(parseInt(scoreVal) <= 0 || parseInt(scoreVal) > 100) {
e.preventDefault();
console.log("Wrong value was entered!");
}
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
<input id="score" type="text" name="score">
<input type="submit">
</form>
@Martijn Bots solution seems fair to me, in case you want to do it otherwise, then you can check specifically for 0.00 and then correct your logic to validate range >=0.01 to 100
function validate()
{
if ( jQuery("#mytext").val() == "0.00"
|| ( $("#mytext").val() >= 0.01 && $("#mytext").val() <=100)
)
{
alert(true);
}
else {
alert(false);
}
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="mytext" type="text" name="score" onblur="return validate();">