Textbox in Firefox cannot CTRL + V (Paste) after some updates - javascript

Before some Firefox update, my app is working fine but after that I cannot CTRL + V (doing Right Click -> Paste is still working fine) anymore. Any idea for this problem?
This is aspx code on the page:
<td width="50%">
<input type="text" maxlength="10" id="RequestID" value="<% =RequestId %>" name="Request_ID"
onload="document.LBS.Request_ID.focus()" onkeypress="if (fnCheckKeyPress(event) == false) {event.returnValue = false; return false;}" />
</td>
The problem is on the "onkeypress" field. If I delete that then it works just fine, but it cannot filter text (it suppose to have only digits) anymore. The function is called from the js file:
function fnCheckKeyPress(event) {
// KeyCode Info: 13-ENTER ; 27-ESC ; 9-TAB ; 8-BACKSPACE ; 46-DELETE
var nKeyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
//alert(nKeyCode);
if (nKeyCode == 13) return false;
if (navigator.userAgent.indexOf("Firefox") != -1) {
if (event.keyCode >= 3 && event.keyCode <= 39) return true;
if (arguments.length > 0 && arguments[0].isChar == false && arguments[0].ctrlKey == true) return true;
}
if (nKeyCode < 46 || nKeyCode > 57) return false;
return true;
}

Related

Preventing User from entering Decimals using JavaScript

I am new to Java Script .I want to prevent the user from entering decimals
in a field using JavaScript. I tried this method but showing this error .
Syntax error on token "_$tag____________", invalid AssignmentOperator
<td>
<form:input class="form-control line_qty_class inputs" onkeypress="checkDecimal();" required="required" id="line_qty${loop.index}" path="saleInvoiceLines[${loop.index}].quantity" />
</td>
<script>
function checkDecimal() {
$(".line_qty").keypress(function(e) {
if (e.which < 46 || e.which > 57) {
showAdvice(this, "Enter Integer Value");
return(false);
}
});
}
</script>
Here you go
$(".line_qty").keypress(function(e) {
if (e.which < 46 || e.which > 57) {
console.log("Enter Integer Value");
return (false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
<input class="line_qty" required="required" id="line_qty" />
</td>
What was wrong?
You assinged to onkyepress function which adds event on keypress. That's pointless. Either just add a function that fires on keypress or use onkeypress,
You wrote in jQuery class line_qty but your element did not have that class. It had class line_qty_class which is different,
You use a function showAdvice() which is not defined, but maybe you have that function defined so it is not a problem.
You can use jQuery keydown() Method to Preventing User from entering Decimals:
$(function() {
$( '#txtinteger' ).on( 'keydown', function( e ) {
-1 !== $.inArray( e.keyCode, [ 46,8,9,27,13,110,190 ] ) || ( /65|67|86|88/.test( e.keyCode ) && ( e.ctrlKey === true || e.metaKey === true ) ) && ( !0 === e.ctrlKey || !0 === e.metaKey ) || 35 <= e.keyCode && 40 >= e.keyCode || ( e.shiftKey || 48 > e.keyCode || 57 < e.keyCode ) && ( 96 > e.keyCode || 105 < e.keyCode ) && e.preventDefault()
} );
} )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtinteger" />

Call to a java script function Regular Expression Validator to fail In asp.Net

I have a <asp:RegularExpressionValidator> that validates a text box but i also have a javascript function that prevents entering of non numerical values in the textbox. when use the expression validator it works fine but as soon as i add onkeydown="return jsDecimals(event);" to the text box to call the jsDecimals() function the validator doesn't work. What am I doing wrong??
asp Code
<asp:TextBox ID="TextBox2" runat="server" CssClass="form-control" CausesValidation="true" MaxLength="13" onkeydown="return jsDecimals(event);"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Retrieve" CssClass="btn btn-default" OnClick="Button5_Click"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" CssClass="tooltip-arrow"
ErrorMessage="ID must be 13 Numeric characters" ControlToValidate="TextBox2" ValidationExpression="^[0-9]{13}$">
</asp:RegularExpressionValidator>
JavaScript:
function jsDecimals(e) {
var evt = (e) ? e : window.event;
var key = (evt.keyCode) ? evt.keyCode : evt.which;
if (key != null) {
key = parseInt(key, 10);
if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
if (!jsIsUserFriendlyChar(key, "Decimals")) {
return false;
}
}
else {
if (evt.shiftKey) {
return false;
}
}
}
return true;
function jsIsUserFriendlyChar(val, step) {
// Backspace, Tab, Enter, Insert, and Delete
if (val == 8 || val == 9 || val == 13 || val == 45 || val == 46) {
return true;
}
// Ctrl, Alt, CapsLock, Home, End, and Arrows
if ((val > 16 && val < 21) || (val > 34 && val < 41)) {
return true;
}
if (step == "Decimals") {
if (val == 190 || val == 110) { //Check dot key code should be allowed
return true;
}
}
// The rest
return false;
}
Your syntax is incorrect. Check my comment on your code below. You do not appear to be closing the function after return true; and you are ending up declaring the next function within the 1st one.
function jsDecimals(e) {
var evt = (e) ? e : window.event;
var key = (evt.keyCode) ? evt.keyCode : evt.which;
if (key != null) {
key = parseInt(key, 10);
if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
if (!jsIsUserFriendlyChar(key, "Decimals")) {
return false;
}
} else {
if (evt.shiftKey) {
return false;
}
}
}
return true; // <-- what's this? is there supposed to be a closing bracket after this line?
function jsIsUserFriendlyChar(val, step) {
.
.
.

Need help restricting a js function(that restricts invalid chars) to a particular input type

Hi i would like to restrict a function that allows only, numbers, back space and left & right arrow keys to inputs with number type, because when i implement it, it also affects my text inputs.
<script>
function chars(evt){
var key = window.event ? event.keyCode : event.which;
if (event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
return true;
}
else if ( key < 48 || key > 57 ) {
return false;
}
else return true;
}
</script>
Assign an id to your <input>. Add an event listener to it, like :
function getKeyCode() {
var key = window.event ? event.keyCode : event.which;
if(event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
console.log(true);
//return true;
} else if (key < 48 || key > 57) {
console.log(false);
// return false;
} else {
console.log(true);
// return true;
}
}
var el = document.getElementById("myInput");
el.addEventListener("keypress", getKeyCode);
<input type="text" id="myInput">

Backspace is not working in Textarea of My Form on Mozila browser

function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
return true;
else
return false;
}
catch (err) {
alert(err.Description);
}
}
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
$('input.facebookUrl').keyup(function(){
if (
($(this).val().length > 0) && ($(this).val().substr(0,24) != 'http://www.facebook.com/')
|| ($(this).val() == '')
){
$(this).val('http://www.facebook.com/');
}
});
<table align="center">
<tr>
<td>
<input name="firstname" class="text_area" maxlength=20 placeholder="Name" type="text" onKeyPress="return onlyAlphabets(event,this)" required id="ValidName" value="" />
</td>
<tr>
<td>
<input type="text" onkeypress="return isNumber(event)" id="volpincode" maxlength=7 name="pincode" value="" required placeholder="Pincode" class="text_area">
</td>
<td><input style="width:300px" type="text" onkeypress="return keyup(event)" class="facebookUrl" name="facebook" value="http://www.facebook.com/$facebook"></td>
<td>
<input style="width:30px" disabled="disabled" type="text" name="facebook" value="+91" />
</td>
</tr>
</tr>
</table>
I am trying to validate the Name and Pin code, as such that in the NAME field only the alphabets have to enter not any other characters and In Pincode only numbers should enter. The form is working in Google chrome as fine, but firefox when i enter and try to delete the text in text field, the characters are not getting deleted.
To validate only alphabets for Name use below code:
$(document).on("keydown", "#NametextboxID", function (e) {
if (e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90) || (key == 9))) {
e.preventDefault();
}
}
});
To validate only numbers for Pin use below code:
$(document).on("keydown", "#PinId", function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) { // if shift, ctrl or alt keys held down
e.preventDefault(); // Prevent character input
} else {
var n = e.keyCode;
if (!((n == 8) // backspace
|| (n == 46) // delete
|| (n >= 35 && n <= 40) // arrow keys/home/end
|| (n >= 48 && n <= 57) // numbers on keyboard
|| (n >= 96 && n <= 105)
|| (n == 9)) // number on keypad
) {
e.preventDefault();
// alert("in if");
// Prevent character input
}
}
});
Add one more condition to the "onlyAlphabets function" for the name field.
if (charCode == 8 || (charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
JS Fiddle Link

How the DEL Key with Mozilla Firefox

I need the help for my problem in my asp.net MVC3 application I have a textbox which is validating for currency format ($228.00) but in this text box it doesn't work for the DEL key because . and delete key have same ASCII key which 46. I also set a validation in this textbox for . is only one time will be accept in the text box so if "." entered one time then delete will not work.
Here is my validate Javascript:
function validateForCharacter(val, id, e) {
window.event.keyCode : -1;
var key = e.keyCode || e.charCode || e.which;
var currentChar = String.fromCharCode(key);
if (val.indexOf(currentChar) != -1 && currentChar == ".")
{
return false;
}
if (key >= 48 && key <= 57 || key == 46 || e.keyCode === 8 || e.keyCode === 9 || e.keyCode === 37 || e.keyCode === 35 || e.keyCode === 39)
{
$(this).val("");
return true;
}
return false;
}
This is my View (Html) code for textbox:
<input type="text" id="t1" onkeypress="return validateForCharacter(value, id, event)/>
Jquery does it right... but in case you need specifically detect delete key on Firefox or IE9 you can use event.key property.
I.E:
if (event.key == "Delete")
Live Demo:
$(document).ready(function() {
$("#nondeletable").on("keydown", function(event) {
$("#result").html(event.type + ": " + event.which);
if (event.key == "Delete")
$("#result").append(" <b>Delete!</b>");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="nondeletable" />
<div id="result"></div>

Categories

Resources