Number : <input type="text" name="inputId" id="quantity" maxlength="11" />
When user enter input it should satisfy below conditions 1. Value can be positive or negative or floating with max length 11
This is what I have written so far
$(document).ready(function () {
//called when key is pressed in textbox
$("#inputID").keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57 || e.which == 45 )) {
//display error message
if(e.which == 45) {
var value = $("#quantity").val();
if(value.indexOf('-') != -1)
{
var index = value.lastIndexOf('-');
if(index = 0){
return false;
}
}
}
else
{
$("#quantity").val("-");
}
}
});
});
I did not consider "." as of now only for negative sign should be at index zero.
is there any better way to do this or modifying this code would be the right way to go.
kindly suggest.