restricting zero is not working for popup - javascript

Hi I want to restrict zero and dot for a field and it is working, But when the field is in pop up then the below code is not working.
<script>
$('#name').keypress(function(e){
if (this.selectionStart == 0 && e.which == 48 || this.selectionStart == 0 && e.which == 46 ){
return false;
}
});
</script>

Since your modal's DOM is generated dynamically on click event, $('.abc').keypress doesn't bind to it (because the modal's DOM doesn't exist yet).
You could make use of event bubbling in such cases. In your case, you could declare the event handler like:
$(document).on('keypress', '.abc', function(e){
if (this.selectionStart == 0 && (e.which == 48 || e.which == 46) ){
return false;
}
});
This means all keypress events on an element with .abc will bubble up to the document, and the event handler will be triggered.
Here's the updated fiddle: http://jsfiddle.net/hcyj3q6q/398/

You are attaching click event on dynamically inserted element. For this
So instead of...
$(document).keypress('.abc', function(e){
if (this.selectionStart == 0 && (e.which == 48 || e.which == 46) ){
return false;
}
});
You can write...
$(document).on('keypress', '.abc', function(e){
if (this.selectionStart == 0 && (e.which == 48 || e.which == 46) ){
return false;
}
});
See this to know more How to add event on dynamically inserted HTML

you can try like this(Javascript)
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode == 48 || charCode == 46
)
return false;
return true;
}
here I have created a fiddle https://jsfiddle.net/vinothsm92/8qde7gnk/2/

Related

jquery clash: “right arrow” and “single quote” getting same keycode in keypress

Similar to this question but a bit different
Javascript keycode clash: "right arrow" and "single quote"
He is using javascript while I am using jquery.
I cannot use keydown event. I want to manage it in keypress event only.
I want to allow Right arrow(& other navigation keys) but disallow entering single quote. both are getting keycode 39.
$(document).ready(function () {
$(".InjectionSafe").keypress(InjectionSafe);
});
function InjectionSafe(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
console.log(charCode);
if (charCode === 9 || charCode == 8 || charCode == 46 // TAB , backspace, delet, left arrow and right arrow
|| charCode == 37 ) { //was pressed
return true;
}
//if (!(charCode != 222 && charCode != 188 && charCode != 190 && charCode!=39)) { // ' < >
if (!(charCode != 60 && charCode != 62 && charCode != 39)) { // ' < >
return false;
}
return true;
}
updated fiddle :https://jsfiddle.net/g2g0sbfo/
Type something & use navigation keys.
Update: I can confirm that this issue is only in firefox(My version is 59.0.1). IE, chrome & opera work fine.
I toyed around with Firefox to see what's happening.
Here is what I got :
You can check originalEvent.key to see which key was actually pressed.
If you pressed ', originalEvent.key = "'".
If you pressed the right arrow, originalEvent.key = "ArrowRight".
$(document).on('keypress', e => {
var charCode = e.which ? e.which : e.keyCode;
$('body').html(charCode);
if (charCode == 39) {
if (e.originalEvent.key == 'ArrowRight') { //Right arrow
$('body').append('<p>Right arrow</p>');
} else {
//Single quote
$('body').append('<p>Single quote</p>');
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Apply custom validation to contact form 7

I am new to wordpress developement.I wabt to apply my own validation to contatc form 7.Adding same in Header.php file but getting no result.
adding this for mobile number validation in header.php file
<script type="text/javascript">
$("#field-mobile").keydown(function(event) { // Allow only backspace and delete
if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9) { // let it happen, don't do anything
} else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
</script>
That is so simple, the script does not work since your code is run before your input text is exist on DOM.
You should add $(document).ready() function then put your block of codes in that function.
I guess this is should work now:
$(document).ready(function() {
$('#field-mobile').bind('keypress', function(e) {
if (e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 9) { // let it happen, don't do anything
} else {
console.log('else block')
if ((e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
}
});
});

Allow float on 'input' not only on 'keypress' event

i have this code to accept valid int or float numbers.
$("input").on("keypress", function(event) {
$(this).val($(this).val().replace(/[^0-9\.]/g, ''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
however, I was not able to catch user input data by copy-paste and rightclick-paste event.
I tried $("input").on("input",... but not working. Please see fiddle demo below.
Fiddle Demo
You can listen to the paste event and check numeric only after a 0ms setTimeout to let the value be pasted in. You'll see a flash of whatever is pasted in so it's not an ideal solution but could be workable.
$("input").on("keypress", function(event) {
$(this).val($(this).val().replace(/[^0-9\.]/g, ''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
$("input").on("paste", function(event) {
var element = $(this);
setTimeout(function(){
element.val(element.val().replace(/[^0-9\.]/g, ''));
}, 0);
});

JQuery : Keypress not working on first press

This is my code
$(currentClass).keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
});
The only problem with this is that, every time I hit a key for the first time regardless a letter or a number, it shows up in my content editable span tag. However, the second time I hit a letter key the code works, it only accepts number. I have no idea what is wrong with this code in the first pressing of keys.
Use JQuery's .on() method.
$(currentClass).on("keypress",function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
});
This is what I used
if ($.inArray($event.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A
($event.keyCode == 65 && $event.ctrlKey === true) ||
// Allow: home, end, left, right
($event.keyCode >= 35 && $event.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// 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();
}
I was able to solve the problem with this. If their suggestion is not working try this.
Bind Properly your events
$(document).ready(function(){
$(body).on("keypress","currentClass",function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
});
});
$(currentClass).on('keypress', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
e.preventDefault();
alert("pressed");
return false;
}
});
event - on
This might helps you :)

javascript : key validation

im using javascript to validate keys in textbox. it is not working :(
function numeric(e) {
return ((e.keyCode == 8) ||
(e.keyCode == 9) ||
(e.keyCode > 47 && e.keyCode < 58) ||
(e.keyCode > 36 && e.keyCode < 41) ||
(e.keyCode == 46) ||
(e.keyCode > 95 && e.keyCode < 106) ||
e.keyCode == 190 ||
e.keyCode == 110);
}
help me...
function numeric(e) {
e = e || window.event;
keycode = e.keyCode || e.which;
if(keycode === 13){
alert("cheese");
}
}
I know that in I.E. you can set event.keyCode=0 to suppress the key appearing in the control. But I think you need to trap the onkeydown. Firefox might have an equivalent. This is good because it prevents the key actually "arriving" at the control.
Also keep in mind that you might need to handle combinations of Shift + key and alt + key.
a good debug technique for this sort of thing is to say windows.status = event.keyCode,
and you can see what the keycode is as you type it...
Just try out the following code. I have checked F5 keycode, you can check as you want
function disableKey(event)
{
if (!event) event = window.event;
if (!event) return;
var keyCode = event.keyCode ? event.keyCode : event.charCode;
if (keyCode == 116) {
showMsg("This functionality is disabled.");
window.status = "F5 key detected! Attempting to disabling default response.";
window.setTimeout("window.status='';", 2000);
// Standard DOM (Mozilla):
if (event.preventDefault) event.preventDefault();
//IE (exclude Opera with !event.preventDefault):
if (document.all && event && !event.preventDefault) {
event.cancelBubble = true;
event.returnValue = false;
event.keyCode = 0;
}
return false;
}
}
function setEventListenerForFrame(eventListener)
{
document.getElementById('your_textbox').onkeydown = eventListener;
//frames['frame'].document.onkeypress = eventListener;
}
<body onload="setEventListener(disableKey);">
Try this if you want a numbers only textbox:
function numbercheck(event) {
var unicode = event.charCode; var unicode1 = event.keyCode; if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Safari") != -1) {
if (unicode1 != 8) {
if ((unicode >= 48 && unicode <= 57) || unicode1 == 37 || unicode1 == 39 || unicode1 == 35 || unicode1 == 36 || unicode1 == 9 || unicode1 == 46)
{ return true; }
else
{ return false; }
}
}
if (navigator.userAgent.indexOf("MSIE") != -1 || navigator.userAgent.indexOf("Opera") == -1) {
if (unicode1 != 8) {
if (unicode1 >= 48 && unicode1 <= 57)
{ return true; }
else
{ return false; }
}
}
}
And in your textbox call it on the onkeypress event:
onkeypress="return numbercheck(event)"

Categories

Resources