I am working on a mobile web application where I need to use a text box which is of type "Number".
<input type="number" name="Mileage" id="txtExample" onkeypress="javascript:return validateNumbers(event,'txtExample');"/>
The problem am getting is here I need to restrict the dot (.
) from being entered in to text box and the javascript method I have written for this is:
function validateNumbers(evt, txtField) {
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else if (charCode == 8)
return true;
}
The charcode of .
is 46 but its going in to true
every time. The strange thing is in desktop website this code is working fine but when running in mobile browser (Chrome, IE) it's not working.
I need assistance guys. This approach don't work in Mobile browser. Thanks in advance.