1

could you please tell me how to make a input field in which user only enter numbers using regex.i am able to do using ascii values .But I need to do this using regular expression

here is my code http://jsfiddle.net/HkEuf/6100/

$(document).ready(function () {
  //called when key is pressed in textbox
  $("#quantity").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
      var BlkAccountIdV = $('#quantity').val();
var re = /[^0-9]/g;    
if ( re.test(BlkAccountIdV) ){  
    console.log("=====")
    errorflag=1;
}else {
   console.log("==tt===") 
   $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
}

     /*if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }*/
   });
});

Thnaks

3 Answers 3

2

This would work:

$(document).ready(function () {
    //called when key is pressed in textbox
    $("#quantity").keyup(function (e) {
        // if letters found flag error, display error, and remove any non-numbers
        if ($(this).val().match(/[^0-9]/g, '')) {
            $("#errmsg").html("Digits Only").show().fadeOut("slow");
            $(this).val($(this).val().replace(/[^0-9]/g, ''));
            console.log("=====")
            errorflag = 1;
        } else {
            console.log("==tt===")
        }
    });
    // but users can still paste values with letters into the box with right click
    // so we bind to paste event and if detected, trigger the element's keyup event
    $("#quantity").bind('paste', function (e) {
        setTimeout(function() { $(this).keyup(); }, 30);
    });
});
#errmsg {
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Number :
<input type="text" name="quantity" id="quantity" />&nbsp;<span id="errmsg"></span>

Or here's a working jsFiddle

Sign up to request clarification or add additional context in comments.

Comments

0
$('input').keyup(function(e){

    if(!/^[0-9]*$/.test(this.value)){

        this.value = this.value.split(/[^0-9.]/).join('');

    }

});

3 Comments

If you are refering to the first fiddle I posted, it was made to demonstrate how validation works and displays true when there are no characters in the input other than numbers (quite correctly).
@DelightedD0D I just demonstrated how to validate using regex which is what was asked. The programmer must know how to bind different events like change if data is pasted in or whatever else might fool the test.
0

I would instead preventDefault the unwanted keyCode on keydown. That way you don't have to do any string manipulation and then replace the input value, which could look awkward, kind I'd like a flash.

$( 'input' ).on( 'click', function ( e ) {
    var numberKeys = [ /* the wanted keyCodes */];
    if ( numberKeys.indexOf( e.keyCode ) < 0 ) {
        e.preventDefault();
        // do what ever you'd like here when the user types in a non numeric character.
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.