I use the following function for decimal validation.the textbox only allow to enter numbers and .(dot) symbol only.It was working fine in IE and Chrome.My problem is I get event is not defined error.How to solve this?I have the lot of textbox validation.so i Create the decimal validation as common its like,
//Call the Function
$('.decimalValidate').live('keypress',function(){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=ApplyDecimalFilter(decimalval);
if(decimalvalidate == false)
return false;
});
//Function for Decimal validation.It allows only one . symbol.everything works fine on IE and Chrome
function ApplyDecimalFilter(id)
{
try {
return NewDecimalFilter(id, event);
} catch (e) {
alert(e.message);
}
}
function NewDecimalFilter(o, event) {
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if ((event.keyCode == 8 || event.keyCode == 46) && o.indexOf('.') == -1) {
return true;
}
return false;
}
And i use this decimalValidate class in text box like as,
input type="text" id="InputLoanAmount" class="decimalValidate" style="width:100px"
In firefox, event isn't global. window.event is undefined. You need to pass the event as a parameter. And by the way, you should use .on instead of .live if you use jQuery >= 1.7.
$('.decimalValidate').live('keypress', function (e) {
var decimalid = $(this).attr("id");
var decimalval = $('#' + decimalid).val();
var decimalvalidate = ApplyDecimalFilter(decimalval, e);
if (decimalvalidate == false) return false;
});
function ApplyDecimalFilter(id, event) {
try {
return NewDecimalFilter(id, event);
} catch (e) {
alert(e.message);
}
}
The live demo.
Related
I've looked on the internet for this and all I can find are depreciated functions so before posting please check to make sure that the code you suggest isn't depreciated.
I've found this and tried it:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent
$(document).ready(function () {
var x = new KeyboardEvent("FormatCode", deprectiatedArgument);
});
But after further inspection the KeyboardEventInit is depreciated.
I would like to create an event on pres of the CTRL + K keys.
You have a specific key code for every button on the keyboard.
All of them are here http://keycode.info/.
$(document).keyup(function(e) {
if (e.keyCode === 13) function(); // enter
if (e.keyCode === 27) function(); // esc
});
Here's a vanilla JS solution to detect a CTRL + k keypress event:
UPDATED to also trigger the event.
document.addEventListener("keypress", function(e) {
if ((e.ctrlKey || e.metaKey) && (e.keyCode == 11 || e.keyCode == 75)) {
alert("ctrl+k!");
}
});
document.getElementById("trigger").addEventListener("click", function(){
//trigger a keypress event...
var e = document.createEvent('HTMLEvents');
e.initEvent("keypress", false, true);
e.ctrlKey = true;
e.keyCode = 75;
document.dispatchEvent(e);
});
Press <kbd>ctrl+k</kbd> or
trigger the event
you can use a library called shortcut.js .. here is a link to their source code for downloading:
http://www.openjs.com/scripts/events/keyboard_shortcuts/shortcut.js
then run ur code by making this function:
shortcut.add("Ctrl+K",function() {
alert("Hi there!");
});
and here is the documentation : http://www.openjs.com/scripts/events/keyboard_shortcuts/
hope that can help.
$(document).ready(function () {
var bool = false;
$(document).keydown(function (e) {
if (e.keyCode === 17) {
bool = true;
}
if (bool == true && e.keyCode == 75) {
alert("");
}
});
$(document).keyup(function (e) {
if (e.keyCode === 17) {
bool = false;
}
});
});
This is how me and a friend got it working
I'm working on a form where I do not want enter/return to submit the form so I used a function like this.
$('[name="form"]').keypress(function(e) {
var charCode = e.charcode || e.keyCode || e.which;
if (charCode === 13) {
e.preventDefault();
return false;
}
});
That works, but now I want to assign the enter/return to perform functions on two inputs on the form. I'm totally stuck.
To get the inputs I've tried vanilla js calling by id, jQ calling by id and then a mixer of the two with variables. I've also tried .keypress, .keydown, .keyup instead of the attachEventListener method. No matter what I do, I get this error in console.
"TypeError: ...addEventListener is not a function" (or keypress, keydown etc.)
I've also researched a good deal but can't find any solution. I appreciate any suggestions.
Here is this block of code in it's current form that's giving the trouble.
var yelpInput = $('#inputURL');
var googleInput = $('#googleURL');
yelpInput.addEventListener("keydown", function(e) {
if (e.keyCode === 13 ) {
alert('do stuff!');
}
});
// Google
googleInput.addEventListener("keydown", function(e) {
if (e.keyCode === 13 ) {
alert('do stuff!');
}
});
Thanks
var yelpInput = $('#inputURL');
var googleInput = $('#googleURL');
yelpInput.keydown(function(e) {
if (e.keyCode === 13 ) {
alert('do stuff!');
}
});
// Google
googleInput.keydown(function(e) {
if (e.keyCode === 13 ) {
alert('do stuff!');
}
});
yelpInput is jQuery wrapped object which does not have addEventListener method.
Use .on to attach event-handler on jQuery wrapped object or yelpInput[0].addEventListener/yelpInput.get(0).addEventListener to attach event using JavaScript as yelpInput[0] will be an DOMElement not jQuery-wrapped object.
var yelpInput = $('#inputURL');
var googleInput = $('#googleURL');
yelpInput.on("keydown", function(e) {
//-----^^^
if (e.keyCode === 13) {
alert('do stuff!');
}
});
googleInput.on("keydown", function(e) {
//-------^^^
if (e.keyCode === 13) {
alert('do stuff!');
}
});
Here is my code
document.onkeydown = function (a) {
if (a.which == 13) {
alert("Not Anymore");
}
}
document.onkeydown = function (b) {
if (b.which == 65) {
auto();
}
}
document.onkeydown = function (c) {
if (c.which == 83) {
auto2();
}
}
Only the last snippet works can someone explain why this is happening
check my website and you can see it isnt working when you press a but when you press b it is
Thanks, I appreciate the help and feedback
You're binding the same event on the document multiple times. So, the later event handlers override the previous event handlers just like the functions with same name does. You need to bind only one event handler and use if... else in it.
You can use this
document.onkeydown = function (e) {
if (e.which == 13) {
alert("Not Anymore");
} else if (e.which == 65) {
auto();
} else if (e.which == 83) {
auto2();
}
};
Also, use addEventListener instead of onkeydown.
document.addEventListener('keydown', function (a) {
if (a.which == 13) {}
...
}, false);
I know this questions is all over the place, but this is driving me crazy!!!
Here is my code:
$(document).ready(function () {
$('#MainContent_LoginUser_Password').keypress(function (e) {
noCapsLock($('#MainContent_LoginUser_Password'), e, "Please turn off Caps Lock");
});
});
function noCapsLock(o, e, str) {
var s = String.fromCharCode(e.which);
if (s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey) {
alert(str);
o.val('');
}
}
I am trying to clear the value of the textbox with the given id. The code above clears the text, but when a new key is pressed, the value of that key is shown (uppercase letters).
I have tried the change(), keyup(), keydown() functions but they still do not seem to clear the textbox of the last value entered.
Any help will be appreciated. Thank you!
You just need to add an event.preventDefault();
You might also want to place your function inside the closure so it isn't global, and you don't need to re-find the html element again inside the method:
$(document).ready(function () {
var noCapsLock = function(o, e, str) {
var s = String.fromCharCode(e.which);
if (s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey) {
alert(str);
o.val('');
e.preventDefault();
}
}
$('#MainContent_LoginUser_Password').keypress(function (e) {
noCapsLock($(this), e, "Please turn off Caps Lock");
});
});
For kicks I also made your code into a jQuery plugin that you can easily apply to any element (it doesn't delete the value just stops the keypress):
(function($) {
$.fn.noCapsLock = function(message) {
this.keypress(function (e) {
var char = String.fromCharCode(e.which);
if (char.toUpperCase() === char && char.toLowerCase() !== char && !e.shiftKey) {
window.alert(message);
e.preventDefault();
}
});
};
})(jQuery);
Apply like this:
$(document).ready(function () {
$('#MainContent_LoginUser_Password').noCapsLock('Please turn off Caps Lock!');
});
You just have to cancel the event with e.preventDefault();:
function noCapsLock(o, e, str) {
var s = String.fromCharCode(e.which);
if (s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey) {
e.preventDefault();
alert(str);
o.val('');
}
}
I would not clear the textbox in your case; if user types long text in lower case, then hits CapsLock and then continues typing - the whole input will be deleted.
As for the function, you can either call event's preventDefault() method or return false (you can read here on the differences between the methods):
$(document).ready(function () {
$('#MainContent_LoginUser_Password').keypress(function (e) {
return noCapsLock(e, "Please turn off Caps Lock");
});
});
function noCapsLock(e, str) {
var s = String.fromCharCode(e.which);
if (s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey) {
alert(str);
return false;
}
return true;
}
I have the following simple javascript code, which handles the Return Key, I don't want to submit the form when the return key is pressed in the textbox.
All this works fine, but in Firefox, if i show an alert message, then it stops working and the form starts getting submitted, whereas the exact code without alert message works fine and stops the form from being submitted. I dont understand why alert is spoiling the party..
$("document").ready(function () {
$("#input1").keydown(OnKeyDown);
});
function OnKeyDown(e) {
if (e.keyCode == 13) {
// alert('this will fail'); // Adding alert makes the form submit
stopBubble(e);
return false;
}
}
function stopBubble (e) {
// If an event object is provided, then this is a non-IE browser
if (e && e.stopPropagation)
// and therefore it supports the W3C stopPropagation() method
e.stopPropagation();
else
// Otherwise, we need to use the Internet Explorer
// way of cancelling event bubbling
window.event.cancelBubble = true;
}
<input type="text" id="input1" value="">
I don't really know if the event is normalized or not. But this is how I have to do it for it to work in all browsers:
$(whatever).keypress(function (e) {
var k = e.keyCode || e.which;
if (k == 13) {
return false; // !!!
}
});
jQuery normalizes this already, you can just do:
$(document).ready(function () {
$("#input1").keydown(OnKeyDown);
});
function OnKeyDown(e) {
if (e.which == 13) { //e.which is also normalized
alert('this will fail');
return false;
}
}
When you do return false from a handler, jQuery calls event.preventDefault() and event.stopPropgation() internally already. You can also do the anonymous function version:
$(function () {
$("#input1").keydown(function() {
if (e.which == 13) return false;
});
});
textBox.onkeydown = function (e) {
e = e || window.event;
if (e.keyCode == 13) {
if (typeof (e.preventDefault) == 'function') e.preventDefault();
if (typeof (e.stopPropagation) == 'function') e.stopPropagation();
if (typeof (e.stopImmediatePropagation) == 'function') e.stopImmediatePropagation();
e.cancelBubble = true;
return false;
}
}