I implemented iphone&android app in jQuery mobile.* i used **pure jQuery mobile
To enter phone `number in a text box. I used TYPE=”TEL”' this is for numaric keypad.
<input type="tel" style=" width:81%;" id="contactNumber" value="" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset" />
But user can enter text also the text box which is unnecessary thing.
How can I prevent user from inserting characters other than numbers?
For mobile devices users it would be much more comfortable if the only input possibile is numeric, as they must press each button to get a number rather than a letter!
I tried adding TYPE=”TEL” TYPE=”mumber”
in the input field, but it does not work to prevent.
$(document).ready(function() {
$('#contactNumber').keyup(function() {
var numbers = $(this).val();
$(this).val(numbers.replace(/\D/, ''));
});
});
This should replace any non-digit with an empty character *as they are put in, negating the need to search for more than one non-digit at a time.
None of the above worked for me but I found a solution that works perfectly for what I needed (to disallow special characters in an input field). Prevents and alerts 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With