only number validation not working for mobile device - javascript

<input class="" name="mobile" type="tel" id="mobile" onkeydown="isNumberKey(this) >
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
//alert(charCode);
if (charCode > 31 && (charCode < 48 || charCode > 57) ) {
return false;
}
else if (charCode == 46 ) { //this charcode is for . on desktop
return false;
}else if(charCode ==229){ //this charcode geeting on mobile device.
return false;
}else {
return true;
}
}
i have written above javascript validation for accepting only number for mobile input tag. it is working fine for desktop, but when i tested on mobile device it is accepting special character "." (dot). it is not going inside of "else if(charCode ==229)" part.
i also checked with input type="number", but same issue for that also.
how to resolve this issue?

You should put your check for charCode == 229 on top of your if-clauses.

Related

jQuery get numeric input from all browsers

I want the user to press on a number key while focus is on an input item, and the browser should go to foo.com/[number]
this is the code I am using:
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
$(document).ready(function(){
$('.my_input').keypress(function(event){
if(isNumberKey(event)){
window.location='foo.com/'+(event.keyCode-48);
}
});
});
It works on Google Chrome, but Firefox users are getting errors. I also want to implement a mobile version where the user can input the number, and press the "enter" key of the mobile device.
How can I ensure that this will work on all browsers that support javascript?
Thanks for any help!
Here's your code corrected but I don't think this is really what you're after. Note that charcode returned from the numpad is different from the that of the number-key row.
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode;
if ((charCode >= 48 && charCode <= 57) || (charCode >= 96 && charCode <= 105)) { //0-9 only (inc numpad)
return true;
}
return false;
}
$(document).ready(function(){
$("input[type='number']").on("keyup",function(evt){
if(isNumberKey(evt)){
var charCode = (evt.which) ? evt.which : evt.keyCode;
//window.location='foo.com/'+(charCode-48);
alert('foo.com/'+(charCode-48));//minus 48 wont work if numpad
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" value="" />
It may be better to just get the value of the input instead...
$(document).ready(function(){
$("input[type='number']").on("keyup",function(){
var val = parseInt(this.value);
this.value="";
if(val>0){
alert('foo.com/'+val);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" value="" />

Allow only numbers in Textbox not working : ASP.NET

I need only numbers in textbox. This is the javascript code that I am using :
function onlyNumbers(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
alert(charCode);
return false;
}
return true;
}
The ASP text box on which I am calling the above function is :
<asp:TextBox ID="txtArtCount" placeholder="Numeric" onKeypress="return onlyNumbers(event);" runat="server" Width="65px" CssClass="searchTextBoxes"></asp:TextBox>
I am using Internet Explorer8 browser. I checked in Chrome as well but it is not working.
On pressing key in textbox, alert is shown but the text gets typed in the textbox as well.
code works correctly , I used input type text instead asp one , check in view source if something renders incorrectly or not
function onlyNumbers(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
alert(charCode);
return false;
}
return true;
}
<input type="text" ID="txtArtCount" placeholder="Numeric" onKeypress="return onlyNumbers(event);" />
hope that helps !

Allow only numbers to be typed in a textbox - default to zero

How can this be modified so that if a user does not enter a number the number 0 will be entered into the text field, thank you in advance:
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
if a user does not enter a number then 0 will be entered into the text field
<input type="text" id="numeric" value="0" onclick="this.value='' " onblur="if(this.value==''){ this.value='0'} " />
regex for numbers only.
$('#numeric').on('input', function (event) {
this.value = this.value.replace(/[^0-9]/g, '');
});
DEMO
Use IsNumeric and check if 0 too.
IsNumeric(number goes here)

PL/SQL NUMBERS ONLY

I am trying to create a numbers only text box in a PL/SQL screen. I am not very familiar with PL SQL or if this would even work.
I cant get this to work. Any suggestions?
htp.p('<html><head>');
htp.p('<script type="text/avascript">
$(function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else
return true;
});
</script>');
htp.p('</head>');
htp.p('<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">');
You are declaring the function inside $(), thus that function is not available to the global scope. To declare it globally, use this:
htp.p('<script type="text/javascript">
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else
return true;
}
</script>');
Should work as expected now.
Original demo (with problem).
Fixed demo (working).

numeric validation not working in firefox

i have a textbox which i need to accept only digits.All other keys in the keyboard sholdn't generate event in the textbox.i created a javascript code
<script type="text/javascript">
function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
html is
<asp:TextBox ID="txtFreeship" runat="server" Width="50px" onkeypress="return onlyNumbers();">
</asp:TextBox>
it is not working in mozilla firefox(working perfectly in internet explorer).can any one please answer
Call the following function on form load
function isDecimalKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && charCode > 46 && (charCode < 48 || charCode > 57)) {
return false;
}
else {
return true;
}
}
txtNoSeat.Attributes["onKeyPress"] = "Javascript:return isNumberKey(event);";

Categories

Resources