javascript - Input text box as positive/negative/floating number with max length 11 -
number : <input type="text" name="inputid" id="quantity" maxlength="11" />
when user enter input should satisfy below conditions 1. value can positive or negative or floating max length 11
this have written far
$(document).ready(function () { //called when key pressed in textbox $("#inputid").keypress(function (e) { //if letter not digit display error , don't type 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 "." of negative sign should @ index zero.
is there better way or modifying code right way go.
kindly suggest.
probably easiest approach use html5 validation:
<body> <form> <input type="text" maxlength="11" pattern="\-?\d+\.?\d+" /> <input type="submit" value="submit" /> </form> </body>
Comments
Post a Comment