jQuery get numeric input from all browsers - javascript

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="" />

Related

only number validation not working for mobile device

<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.

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 !

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);";

WebtextEdit execute Javascript function

I need to execute an entire javascript function.
Found what I was doing wrong:
var edit = igedit_getById("FG2100RefNo");
var editText = edit.getText();
alert(!isNaN(editText));
But, this only checks & displays a message box.
I need to execute the below javascript:
<script language=Javascript>
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
//-->
onkeypress="return isNumberKey(event)"
How can I do this with the webtextedit:
var edit = igedit_getById("FG2100RefNo");
var editText = edit.getText();
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
return true;
}
return isNumberKey(editText)
I only have the onkey event property value to insert the code.
You can provide the client function name to the control's ClientSideEvents.KeyPress property from the server-side.
C#:
FG2100RefNo.ClientSideEvents.KeyPress = "isNumberKey";
you'll have to update the definition of isNumberKey to accept the parameters provided and use the control's method of canceling the action.
ClientSideEvents reference:
http://help.infragistics.com/Help/NetAdvantage/ASPNET/2009.1/CLR3.5/html/Infragistics35.WebUI.WebDataInput.v9.1~Infragistics.WebUI.WebDataInput.WebTextEdit~ClientSideEvents.html
Your onkeypress="return isNumberKey(event)" function is sufficient to force user type only numerics.

Categories

Resources