admin管理员组

文章数量:1416642

I have a textfield of Price.

I want only Integer and Float values in it.

I have done the Integer. But it is not accepting Float values like : 3110.6

Here is my Code DEMO

JS:

$(document).ready(function () {
    $("#price").keydown(function (event) {
        // Allow only backspace and delete
        if (event.keyCode == 46 || event.keyCode == 8) {
            // let it happen, don't do anything
        } else {
            // Ensure that it is a number and stop the keypress
            if (event.keyCode < 48 || event.keyCode > 57) {
                event.preventDefault();
            }
        }
    });
});

HTML:

<input name="price" type="text" id="price">

I have a textfield of Price.

I want only Integer and Float values in it.

I have done the Integer. But it is not accepting Float values like : 3110.6

Here is my Code DEMO

JS:

$(document).ready(function () {
    $("#price").keydown(function (event) {
        // Allow only backspace and delete
        if (event.keyCode == 46 || event.keyCode == 8) {
            // let it happen, don't do anything
        } else {
            // Ensure that it is a number and stop the keypress
            if (event.keyCode < 48 || event.keyCode > 57) {
                event.preventDefault();
            }
        }
    });
});

HTML:

<input name="price" type="text" id="price">
Share Improve this question edited Oct 31, 2013 at 7:04 Tushar Gupta - curioustushar 57.1k24 gold badges106 silver badges109 bronze badges asked Oct 31, 2013 at 7:02 Hassan SardarHassan Sardar 4,52317 gold badges61 silver badges92 bronze badges 3
  • if(isNaN(parseFloat(...)) || isNaN(parseInt(..., 10)) – Zeta Commented Oct 31, 2013 at 7:06
  • 1 <input type="number" ... – adeneo Commented Oct 31, 2013 at 7:06
  • Update fiddle please ? – Hassan Sardar Commented Oct 31, 2013 at 7:42
Add a ment  | 

4 Answers 4

Reset to default 2

try this demo

 $('#price').keypress(function(event) {
            if(event.which == 8 || event.which == 0){
                return true;
            }
            if(event.which < 46 || event.which > 59) {
                return false;
                //event.preventDefault();
            } // prevent if not number/dot

            if(event.which == 46 && $(this).val().indexOf('.') != -1) {
                return false;
                //event.preventDefault();
            } // prevent if already dot
        });

Simply use this:

<input name="price" type="number" id="price">

And remove your JavaScript code.

Try regular expression

/^[+-]?\d+(\.\d+)?$/

Just test the value of the input on the onchange event.

TRy Demo dot or period has keycode 190

$(document).ready(function() {
$("#price").keydown(function(event) {
    // Allow only backspace and delete
    if ( event.keyCode == 46 || event.keyCode == 8 ||  event.keyCode == 190 ) {
        // let it happen, don't do anything
    }
    else {
        // Ensure that it is a number and stop the keypress
        if (event.keyCode < 48 || event.keyCode > 57 ) {
            event.preventDefault(); 
            }   
        }
    });
});

But better will be use jquery validator plugin at http://jqueryvalidation/ as you dont have to check yourself.

For jquery version see JQUERY VALIDATION

本文标签: javascriptAlow only Integer and Float values in TextfieldStack Overflow