Check numeric text box in jquery

I have this code in jquery to prevent the input of non-numeric characters in a text box

$("#NumericField").numeric();

Now, in the text box, I cannot enter non-numeric characters. Everything is fine. The problem here is that the user will insert odd characters into the text box.

Is there a way / method to disable insertion if the value is not numeric? Or is there any other approach to solving this situation that you can use?

+5
source share
3 answers

you can use a callback that checks the outgoing field, if the value is valid, if the value is invalid, then clear it and show the error message:

var decimal_char = ',';
function isvalidnumber(){
    var val=$(this).val();
    //This regex is from the jquery.numeric plugin itself
    var re=new RegExp("^\\d+$|\\d*" + decimal_char + "\\d+");
    if(!re.exec(val)){
        alert("Invalid number");
        $(this).val("");
    }       
}
$(document).ready(function(){
    $("#txtN").numeric(decimal_char,isvalidnumber);
});
+5

:

value = value.replace(/\./, "");

:

value = value.replace(/\.\,/, "");

; -)

:

$(‘.InputClass’).keyup(function(){
  if ($(this).val() != "")
     $(this).val( $(this).val().replace(/[^0-9\.]/g, "") );
});

, ; - , , . , .

. -.

+1

script http://www.kunalbabre.com/jQueryLibrary/index.php:

$('input[numeric]').keyup(function() {    
    var d = $(this).attr('numeric');

    var value = $(this).val();
    var orignalValue = value;
    value = value.replace(/[0-9]*/g, "");

    var msg = "Only Integer Values allowed.";

    if (d == 'decimal') {
        value = value.replace(/\./, "");
        msg = "Only Numeric Values allowed.";
    }

    if (value != '') {
        orignalValue = orignalValue.replace(/([^0-9].*)/g, "")
        $(this).val(orignalValue);
        //alert(msg);
        $(this).after('<span style="margin-left:5px;color:red;position:absolute;">' + msg + '</span>');
    } else {
        $(this).next('span').remove();
    }
});

, , "," "100 000,00". "100". "", .

: , reg ex, .: (

? ?

0

All Articles