How can I prevent a user from inserting characters into a text field other than a number Only numbers can embed jQuery Mobile

I have implemented iphone and android application in jQuery mobile. * I used ** pure jQuery mobile

Enter your phone number in the text box. I used TYPE = "TEL", this is for a numeric keypad.

<input type="tel" style=" width:81%;" id="contactNumber" value="" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset" /> 

But the user can enter text and a text field that is not necessary.

How can I prevent user from inserting characters other than numbers?

It would be much more convenient for users of mobile devices if the only input option was numeric, since they had to press each button to get a number, not a letter!

I tried adding TYPE="TEL" TYPE="mumber"to the input field, but it does not work to prevent.

+5
source share
6 answers
$(document).ready(function() {
    $('#contactNumber').keyup(function() {
        var numbers = $(this).val();
        $(this).val(numbers.replace(/\D/, ''));
    });
});

This should replace any non-digit with an empty *, as they are placed in it, which negates the need to search for more than one minor number at a time.

+9
source

None of the above worked for me, but I found a solution that works fine for what I need (to disable special characters in the input field). Prevents and warns the user.

    <script>
     $("#userlogin").keypress(function(e) {
      if (String.fromCharCode(e.which).match(/[^A-Za-z0-9_ ]/)) {
        e.preventDefault();
        alert("Special characters are not allowed. Use 'A-Z', 'a-z' and '0-9'.");
       }
     });
    </script>
+6
source

, . http://www.texotela.co.uk/code/jquery/numeric/

:

$(document).ready(function(){
    $(".numeric").numeric();
});

jQuery, onkeyup

: http://www.htmlcodetutorial.com/forms/index_famsupp_158.html

step:

http://www.w3.org/TR/html-markup/input.number.html#input.number.attrs.step.float

<input type="number" step="0.01" min="0" >
+1

type = "tel" - HTML 5, jqueryMobile. , HTML 5, . . .

+1
$('#contactNumber').bind('keypress', function (event) {
        event = event || window.event; canAdd = new Boolean(false);
        canAdd = ((event.charCode > 47) && (event.charCode < 58) || (event.charCode == 32) || (event.charCode == 40) || (event.charCode == 41) || (event.charCode == 43) || (event.charCode == 45) || (event.charCode == 124));
        if (!canAdd && event.charCode)
            return false;
    }); 

HTML5 tel attrubute

0

HTML:

 input type="tel" (keypress)="isNumberKey($event)" 

Script:

isNumberKey(evt: any) { < br >
        // to accept only numbers in contact field<br>
        if ( < br >
            (evt.key >= '0' && evt.key <= '9') || < br >
            evt.key == "Backspace" || < br >
            evt.key == "Delete" || < br >
            evt.key == "ArrowLeft" || < br >
            evt.key == "ArrowRight" < br >
        ) { < br > //"Backspace" etc for 'firefox' browser<br>
                return true; < br >
        } < br >
        return false; < br >
}
0
source

All Articles