In JavaScript code how to allow exception for tab and backspace.
On keypress event
onkeypress="return isNumberKey(event, this)"
Code is
function isNumberKey(evt, el) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && ((charCode < 48 || charCode > 57)) return false;
if (el.value.length > 2)
return false;
return true;
}
}
This solved my problem,
I added exception in second condition
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
if (el.value.length >= 2 && charCode != 8)
return false;
return true;
<script>
function isNumberKey(evt, el) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && ((charCode < 48 || charCode > 57))) return false;
if (el.value.length > 2)
return false;
return true;
}
</script>
<input type="text" id="text" onkeypress="return isNumberKey(event, this)">
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="" />
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 !
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).
I have this JavaScript function that is used to force user only type number in the textbox. Right now and I want to modify this function so it will allow the user to enter plus (+) symbol. How to achieve this?
//To only enable digit in the user input
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Since the '+' symbol's decimal ASCII code is 43, you can add it to your condition.
for example :
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode != 43 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
This way, the Plus symbol is allowed.
This code might work. I added support for SHIFT + (equal sign) and the numpad +.
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
var shiftPressed = (window.Event) ? e.modifiers & Event.SHIFT_MASK : e.shiftKey;
if ((shiftPressed && charCode == 187) || (charCode == 107))
{
return true;
} else if ((charCode > 95) && (charCode < 106)) {
return true;
} else if (charCode > 31 && (charCode < 48 || charCode > 57))) {
return false;
} else {
return true;
}
}
this is stupid ... not really an answer at all. I would suggest you to do following.
function isNumberKey(evt)
{
console.log(evt.keyCode);
return false;
}
And find out the ranges of all keys, and implement it.
It's Work. Javascript keycode allow number and plus symbol only
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript form validation</title>
</head>
<body>
<form name="form1" action="#">
Mobile Number: <input type='text' id='PhoneNumber' maxlength="10" onKeyPress="return IsNumeric3(event);" ondrop="return false;" onpaste="return false;"/>
<span id="error3" style="color: Red; display: none">* Input digits (0 - 9)</span>
</form>
<script type="text/javascript">
var specialKeys = new Array();
specialKeys.push(8);
specialKeys.push(43);
specialKeys.push(37);
specialKeys.push(39);
//Backspace
function IsNumeric3(e) {
var keyCode = e.which ? e.which : e.keyCode
var ret = (keyCode != 37 && keyCode != 8 && keyCode != 46 && (keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
document.getElementById("error3").style.display = ret ? "none" : "inline";
return ret;
}
</script>
<script>
function stringlength(inputtxt, minlength, maxlength)
{
var field = inputtxt.value;
var mnlen = minlength;
var mxlen = maxlength;
if(field.length<mnlen || field.length> mxlen)
{
alert("Please input the 10 digit mobile number");
return false;
}
else
{
return true;
}
}
</script>
</body>
</html>
Thank you friends
Here is the modified code:
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if ( (charCode >= 48 && charCode <= 57) || charCode == 43)
return true;
return false;
}
Here is the code . working fine with numbers and plus + sign in phone fields. you will have to implement the code on keydown function . target the id/class of the particular phone field and use keydown function.
//allows only these keys
// backspace, delete, tab, escape, and enter
if ( event.keyCode == 107 || event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
// Ctrl+A
(event.keyCode == 65 && event.ctrlKey === true) ||
// home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
return;
}
else {
// Ensure that it is a number and stop the keypress
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
event.preventDefault();
}
}
Using experience of my colleagues above, write one function, that fits me well. It filters all except numbers, arrows and backspace. Maybe it would be useful for somebody.
function isKeyCorrect(keyEvent) {
var charCode = keyEvent.which ? keyEvent.which : keyEvent.keyCode;
var isNotNumber = charCode < 48 || charCode > 57;
var isNotArrow = charCode < 37 || charCode > 40;
var isNotBackspace = charCode !== 8;
return isNotNumber && isNotArrow && isNotBackspace;
}
<script type="text/javascript">
$(document).ready(function() {
`enter code here` $('#form-1').submit(function(msg) {
$.post("action.php?act=login",$(this).serialize(),function(data){
if (data == 'ERR:A3001004') { alert("Güvenlik hatası, sayfayı yenileyin."); }
else if (data == 'TIMEEND') { alert("Anahtarınızın süresi dolmuş."); }
else if (data == 'INVALID') { alert("Geçersiz anahtar şifresi girdiniz."); }
else if (data == 'OK') { alert("Başarıyla giriş yaptınız. Yetişkinlere göre içerik barındıran sitelere erişim sağlayabilirsiniz."); }
});
return false;
});
});
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>