I'm struggling to prevent input of alt + numpad unicode characters.
The alt key doesn't seem to register on keyup and will enter the unicode character regardless. (try something like 'alt + 1' in the example code snippet below to see what I mean.)
I've tried something like the following that attempts to restrict non-numeric characters:
$("#myInput").on('paste keyup keydown change', function(event) {
var $input = $(this);
var value = $input.val();
// remove whitespace
value = value.replace(/\s+/g, '');
// remove unwanted characters
value = value.replace(/[^0-9]+/g, '');
$input.val(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="myInput" type="text">
Is there an event I should be looking for instead of the above 4? (paste keyup keydown change)
I was able to prevent them on the keypress event...
function noteKeyPress(event) {
if(event.code.slice(0,3) == 'Alt'){
event.preventDefault();
event.stopPropagation();
}
}
This page was a great help
Related
Every time user enter, value is checked with regular expression, I'm trying to restrict user from entering further into input field if regexp is not matched
Using keyup event, preventdefault never fires and using keypress event, user is unable to input at all because in the begining, value in input field shows as "" (nothing)
var discountRegex = /(^100([.]0{1,2})?)$|(^\d{1,2}([.]\d{1,2})?)$/
$("#" + (idOfElement)).on("keyup",function (e) {
var val=this.value
var k = e.keyCode
if(k==46 ||(k > 48 && k <97)){
console.log(k)
return discountRegex.test(val);
}
});
in the above code idOfElement is the id i get on whichever field i focus.
Please refer sample code. If input key is invalid input will not accept it. Also please find fiddle for same in comment.
<input type="text">
$(document).ready(function(){
$("input").bind('keypress', function(e) {
var str = e.keyCode;
if (/(^100([.]0{1,2})?)$|(^\d{1,2}([.]\d{1,2})?)$/.test(str)) {
alert('Invalid')
e.preventDefault();
} else {
alert('Valid');
}
});
});
You can check if the regex is matched and if not you can remove the last char like the example below
I updated the code with keydown example
Example
Code:
$j("#<%= txtGradingScale.ClientID%>").bind("keypress", function (e)
{
var keyed = $j(this).val();
$j("#<%= txtGradingScale.ClientID%>").html
(keyed.replace(/\<>/gi, ''));
});
Have to restrict greter than and lesser than symbol in textbox while entering .
above code is not working pls suggest the method .i tried keyCode and Charcode but it's not working
The reason it's not working is because the regular expression /\<>/ (the escape character \ is not needed) is looking for <> and not the characters by themselves, what you want to do is:
$('textarea[name="test"]').keyup(function(e) {
$(this).val($(this).val().replace(/[<>]/ig, ''));
});
This will match any instance of the < and > characters no matter what order they appear in.
You should also use keyup instead of keypress because keypress will only trigger after the next key gets hit, while keyup will trigger whenever the key is released.
Fiddle
You want to test using e.which and compare with corresponding codes for < and >. If you return false or invoke e.preventDefault() when they are encountered, that should do it.
$('#myText').on('keypress', function(e) {
if( e.which === 60 || e.which === 62 ) {
e.preventDefault();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name="myText" id="myText"/>
I am using following validation to put autodash in my textbox .
$('input#txtReportingNum').keyup(function (e) {
this.value = this.value
.match(/\d*/g).join('')
.match(/(\d{0,2})(\d{0,2})(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
});
Input = string_1-String_2-String_3
The above code does not allow me to enter alphabets . How can i change the above pattern to make String_2 and String_3 to allow both alphabets and numbers. Can anybody please help me to get this done ?
I want to make something like 89-e3-w323
Try something like this
$('input#txtReportingNum').keyup(function (e) {
this.value = this.value
.match(/[a-fA-F0-9]*/g).join('')
.match(/(\d{0,2})([0-9][a-fA-F])(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
});
Input = string_1-String_2-String_3
I have several input fields that I need to filter the input on.
I've put them in a class and used a regular expression to limit the characters to numbers, letters, and underscore.
That works fine, but when I tab from one input field to the next, the cursor moves to the end of the input text. I want it to be highlighted so that it can be typed over if desired instead of having to highlighting it with the mouse first.
<input type="input" class="jqfc" value="one"><br>
<input type="input" class="jqfc" value="two"><br>
<input type="input" class="jqfc" value="three"><br>
<input type="input" value="highlights"><br>
jQuery('.jqfc').keyup(function () {
this.value = this.value.replace(/[^a-z0-9\_]/gi, "");
});
sample:
http://jsfiddle.net/ngwr6/2/
jQuery('.jqfc').keyup(function (e) {
if (e.keyCode !== 9){
this.value = this.value.replace(/[^a-z0-9\_]/gi, "");
}
});
This way it wont run the logic if the tab key is pressed. I thought of doing something like select(), but then that happens every time you type.
This ought to do the trick:
jQuery('.jqfc').keyup(function () {
var regex = /[^a-z0-9\_]/gi;
if(this.value.match(regex)){
this.value = this.value.replace(regex, "");
}
});
jQuery('.jqfc').on('focus, click', function(){
this.select();
});
http://jsfiddle.net/ngwr6/5/
I need to prevent the single and double quotes from being pasted in a text area in javascript
HTML
<textarea rows="10" cols="10" id="txtTest"></texarea>
Preventing the single and double quotes on keydown
$('#txtTest').on('keydown', function(e){
if(e.shiftKey && e.keyCode == 222 || e.keyCode == 222){
e.stopPropagation();
}
});
How to prevent the same when the text is pasted. (ctrl + v)
You'll need to trap more than the keydown event - Ctrl+V or right-click and paste can also insert characters (well and the older Shift+Insert). Depending on your usage, you may want to remove unwanted characters before submission. However, if you want it on the event, then trap all events and replace the text with the cleared text, depending on the event type.
$('#txtTest').val($('#txtTest').val().replace(/['"]/g, ''));
That'll remove all the single and double quotes (though not "smart" quotes).
may be this: http://jsfiddle.net/patelmilanb1/7NfLV/1/
$('#txtTest').on('keypress', function (e) {
var ingnore_key_codes = [34, 39];
if ($.inArray(e.which, ingnore_key_codes) >= 0) {
e.preventDefault();
}
});
listener to input event
var t = document.getElementById("text");
t.addEventListener("input", function (){
var str = this.value;
if (str.search(/'|"/g) !== -1) {
alert("\'\"is not allowed");
}
}, false);
demo: http://jsfiddle.net/EZXqH/
Besides coding the logic you have to think how to invoke the code at proper time. Here is a demonstration for that. On every system-paste event(either mouse or key board) the following code will run and do the operation. No need of any ASCII code.
Use the following:
$('textarea').on('paste', function () {
var element=$(this);
setTimeout(function () {
element.val(element.val().replace(/['"]/g, ""));
}, 1);
});
Check Fiddle