I want to add the code to enable ctrl+c and ctrl+v in the function, the numbers are enabled and the alphabets are disabled. This function is used in the textbox, where the ctrl+c and ctrl+v are to be enabled but not the alphabets. I tried various things, but could not get through it. Any help in what do I modify in code to make them enable?
function item_checkInput(object, e, len, nr) {
var keycode, date, retval, value, newvalue, sel;
if (window.event) {
keycode = window.event.keyCode;
range = window.event.RangeOffset;
}
else if (e.which != 0) {
keycode = e.which;
range = e.rangeOffset;
}
else {
keycode = e.keyCode;
range = e.rangeOffset;
}
date = new Date();
value = object.value.replace(/\./g, "");
if ( (keycode >= 48 && keycode <= 57) && e.rangeOffset != undefined )
newvalue = value.substr(0,e.target.selectionStart) + String.fromCharCode(keycode) + value.substr(e.target.selectionEnd);
else if ( (keycode >= 48 && keycode <= 57) && document.selection )
{
document.selection.createRange().text = String.fromCharCode(keycode);
newvalue = object.value;
object.value = value;
}
else
newvalue = value;
if (!NotallNumbers(e)) {
return false;
}
if ( (keycode == 9) ) //tab (9)
{
if (newvalue.length == 13 && newvalue.substr(0, 7) == "10000") {
newvalue = newvalue.substr(7);
}
if (newvalue.length == 6) {
retval = check_itemnr(newvalue, nr);
if (retval == false) {
object.value = newvalue;
focusEnSelect(object, true);
}
object.value = newvalue;
}
return false;
}
else if ( (keycode == 8) || (keycode == 46) || (keycode >= 35 && keycode <= 40) )
return true;
else if ( (keycode >= 48 && keycode <= 57) && ((date.getTime() - itemnr_keypress) > interval_scan) ) {
if (newvalue.length == 13 && newvalue.substr(0,7) == "10000")
newvalue = newvalue.substr(7);
if ( (newvalue.length == len) ) {
retval = artnr_checknr_art(newvalue, nr);
if (retval == false) {
object.value = newvalue;
focusEnSelect(object, true);
} else {
object.value = newvalue;
return false;
}
return retval;
}
itemnr_keypress = date.getTime();
return true;
}
else if ( (keycode >= 48 && keycode <= 57) && (newvalue.length == 6) && (newvalue != "10000") ) {
itemnr_keypress = date.getTime();
retval = check_itemnr(newvalue, nr);
if (retval == false) {
object.value = newvalue;
focusEnSelect(object, true);
}
return retval;
}
else if ( (keycode >= 48 && keycode <= 57) && (newvalue.length == 13) ) {
itemnr_keypress = date.getTime();
if (value.substr(0, 7) == "10000") {
object.value = newvalue.substr(7);
retval = check_itemnr(newvalue.substr(7), nr);
if (retval == false) {
focusEnSelect(object, true);
}
return false;
}
else {
object.value = "";
return false;
}
return true;
}
else if (keycode >= 48 && keycode <= 57) {
itemnr_keypress = date.getTime();
return true;
}
else if (keycode == 106) {
retval = check_itemnr(newvalue, nr);
if (retval == false) {
object.value = newvalue;
focusEnSelect(object, true);
}
return false;
} else if (keycode == 13) {
if (newvalue.length == 13 && newvalue.substr(0, 7) == "10000") {
newvalue = newvalue.substr(7);
}
if (newvalue.length == 6) {
retval = check_itemnr(newvalue, nr);
if (retval == false) {
object.value = newvalue;
focusEnSelect(object, true);
}
object.value = newvalue;
}
return false;
} else if (newvalue.length > 13) {
return false;
}
return false;
}
Related
I'm trying to make the input takes only the value 99.999. I don't want to use MaxLength because it would not calculate the length of the decimal digits. I don't want to use any other functions that erase when it doesn't match a specific regex. I want it to stop it in the input.
function IsCurrencyNoMinus1 (e, thisobj, min, max) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (keyCode == 46) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val();
if (ret && (keyCode == 45) && ((thisobj.selectionStart != 0) || (inStr.indexOf('-') != -1)))
ret = false;
if (ret && (keyCode == 46) && (inStr != '' && inStr.indexOf('.') != -1) && !(Math.abs(thisobj.selectionStart - thisobj.selectionEnd) == inStr.length)) {
ret = false;
}
var dotPos = (inStr.indexOf('.') != -1) ? inStr.indexOf('.') : inStr.length;
inStr = inStr.replace(/\,/g, '');
var parts = inStr.split('.');
var maxParts = max.toString().split('.');
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57))) {
if ((parts[0].length >= maxParts[0].length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart <= dotPos)) {
ret = false;
}
if (ret && (parts[1] != undefined && parts[1].length >= 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart > dotPos) && (thisobj.selectionStart <= dotPos + 3))
ret = false;
var firstPos = thisobj.selectionStart < thisobj.selectionEnd ? thisobj.selectionStart : thisobj.selectionEnd;
if (ret && (parts[0].length >= maxParts[0].length) && (parts[1] != undefined && parts[1].length >= 1)
&& ((dotPos - firstPos == 0 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4)
|| (dotPos - firstPos == 1 && (Math.abs(thisobj.selectionStart - thisobj.selectionEnd) >= 2 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4))))
ret = false;
}
if (Number(inStr) > max) {
thisobj.value = '';
ret = true;
}
if (Number(inStr) < min) {
thisobj.value = '';
ret = true;
}
// var re = new RegExp(/^\(?-?[0-9]{0,12}(\.[0-9]{0,2})?\)?$/)
// if (!re.test(inStr)) {
// thisobj.value = ""
// }
return ret
}
I found the solution! Please check the code below in case someone needs it.
function Format3DigitDecimal(e, thisobj, min, max)
{
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val()
inStr = inStr.replace(/\,/g, '')
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length >= max.toString().length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length == 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
return ret
}
I have a textbox that I want it to allow typing numbers from 0.00 to 24.00 only.
<asp:TextBox ID = "txtHours" runat = "Server"
onkeydown="limit(this,event);"
onkeypress="return validateFloatKeyPress(this,event);"
MaxLength="5" Width = "40" text = '<%#showData(Container.DataItem, "Hours")%>'/>
I tried using Javascript in onkeypress and onkeydown
It's still allowing typing numbers like 25,26,25.01... etc
I want it not to even allow typing 5,6,7...etc after 2 is typed.
function validateFloatKeyPress(el, evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var number = el.value.split('.');
var numberbfr = el.value.split('.')[0];
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
if (number.length > 1 && charCode == 46) {
return false;
}
if (numberbfr.length > 1) {
return false;
}
var caratPos = getSelectionStart(el);
var dotPos = el.value.indexOf(".");
if (caratPos > dotPos && dotPos > -1 && (number[1].length > 1)) {
return false;
}
return true;
}
function getSelectionStart(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveEnd('character', o.value.length)
if (r.text == '') return o.value.length
return o.value.lastIndexOf(r.text)
} else return o.selectionStart
}
function limit(el, evt) {
if (parseInt(el.value.charAt(0)) > 2 || (parseInt(el.value.charAt(0)) = 2 && parseInt(el.value.charAt(1)) > 4)) {
return false;
}
}
Try this.Hope this solution will help you.Copy and paste below code.'stack' is the id of the textbox.
function checkOnlyZeroTo24(evt, el) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (el.value == '' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
return true;
}
else if (el.value != '') {
if (el.value.charAt(0) < 3 && charCode == 46 && el.value.length == 1 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.length == 1) {
document.getElementById('stack').setAttribute('maxlength', 5);
if (el.value.charAt(0) == 2 && (charCode < 48 || charCode > 52))
return false;
else
return true;
}
else if (el.value <= 24 && el.value >= 10 && charCode == 46 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 5);
return true;
}
else if (el.value.substr(0, el.value.indexOf('.')) <= 24 && el.value.substr(0, el.value.indexOf('.')) >= 10 && el.value.charAt(2) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
if (el.value.substr(0, el.value.indexOf('.')) == 24 && !(charCode == 48))
return false;
else
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.charAt(1) == '.') {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if ((el.value.charAt(0) >= 3 && charCode == 46 && el.value.indexOf('.') == -1) || (el.value.charAt(1) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57)))) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
}
return false;
}
Asp:TextBox
<asp:TextBox ID = "stack" runat = "server" onkeypress="return checkOnlyZeroTo24(event,this);"> </asp:TextBox>
I have a problem. Basically, what happens in my case is that the numbers in my textbox are autoformatted as I type. I don't want this to happen. What I want is that the numbers should be autoformatted only when the user clicks outside the textbox.
In my input tag I have :
onkeyup="format(event, this);"
My javascript function is :
function format(e, obj) {
if (e.keyCode == 36) {
press1(obj);
}
if (e.keyCode == 13) {
return false;
}
if ((e.keyCode <= 34) || (e.keyCode >= 46 && e.keyCode < 58) || (e.keyCode >= 96 && e.keyCode <= 105)) { // //alert(e.keyCode);
obj.value = CommaFormatted(obj.value);
} else {
if (e && e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
} else {
e.cancelBubble = true;
e.returnValue = false;
}
return false;
}
}
where the press1 function is:
function press1(textControlID) {
var text = textControlID;
if (text.getAttribute("maxlength") == text.value.length) {
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
return true;
}
if (text != null && text.value.length > 0) {
if (text.createTextRange) {
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
} else if (text.setSelectionRange) {
var textLength = text.value.length;
text.setSelectionRange(textLength, textLength);
}
}
}
I really hope this could be solved. Please!
You could change onkeyup to onblur, which is the event that gets fired when the control loses focus - clicking out of it.
The onkeyup event fires with every keypress.
In the below code i want to allow alphaets and to restrict special characters.But it is allowing special characters.pls help me to solve this issue.
JS:
function AcceptAlphabetsOnly(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);
}
};
Asp.net
<input name="data[Customer][name]" type="text"
id="txtVendor" runat="server" onkeypress="return AcceptAlphabetsOnly(event,this);" />
Try the below function
function checkSpcialChar(event){
if (!((event.keyCode >= 65) && (event.keyCode <= 90) || (event.keyCode >= 97) && (event.keyCode <= 122) || (event.keyCode >= 48) && (event.keyCode <= 57))) {
event.returnValue = false;
return;
}
event.returnValue = true;
}
I'm building a terminal emulation and running into an issue with capturing backspace in Firefox. I'm able to nab the first backspace and remove the last character on the input at the prompt, but it won't persist and remove more than one character.
Actual website: http://term.qt.io/
Replication here: http://jsfiddle.net/BgtsE/1/
JavaScript code
function handleKeys(e){
var evt = e || window.event;
var key = evt.charCode || evt.keyCode;
if(evt.type == "keydown")
{
curr_key = key;
if(key == 8)
{
evt.preventDefault();
if(0 < $('body').text().length)
$('body').text($('body').text().slice(0,-1));
}
}
else if(evt.type == "keypress")
{
if(97 <= key && key <= 122)
{
if(curr_key != key)
$('body').append(String.fromCharCode(key));
}
else
$('body').append(String.fromCharCode(key));
}
}
$(function(){
$('html').live({
keydown:function(e){
handleKeys(e);
},
keypress:function(e){
handleKeys(e);
}
})
})
Try this: http://jsfiddle.net/NBZG8/1/
You'll need to handle backspace in both keydown and keypress to support Chrome and Firefox
function handleKeys(e){
var evt = e || window.event;
var key = evt.charCode || evt.keyCode;
if (evt.type == "keydown") {
curr_key = key;
if(key == 8 && !$.browser.mozilla) {
backspaceHandler(evt);
}
} else if (evt.type == "keypress") {
if (key == 8) {
backspaceHandler(evt);
} else if (97 <= key && key <= 122) {
if(curr_key != key) {
$('body').append(String.fromCharCode(key));
}
} else {
$('body').append(String.fromCharCode(key));
}
}
}
function backspaceHandler(evt) {
evt.preventDefault();
if(0 < $('body').text().length) {
$('body').text($('body').text().slice(0,-1));
}
};
$(function(){
$('html').live({
keydown : handleKeys,
keypress : handleKeys
})
})
In firefox Windows 17.0.1 any value returned by $("selector").text() has an added new line character appended to the end. So the substring didn't work for me:
<html>
<head>
<title>test</title>
<script src="jquery.js"></script>
<script>
$("document").ready(function(){
console.log("body text seems to have a new line character");
console.log(($('body').text()[5]=="\n"));
});
function handleKeys(e){
var evt = e || window.event;
var key = evt.charCode || evt.keyCode;
if(evt.type == "keydown")
{
curr_key = key;
if(key == 8)
{
evt.preventDefault();
if(0 < $('body').text().length)
// next line works, you might trim the \n if it's there at the end
//$('body').text($('body').text().slice(0,-2));
// this one didn't work for me
$('body').text($('body').text().substring(0,$('body').text().length-1));
}
}
else if(evt.type == "keypress")
{
if(97 <= key && key <= 122)
{
if(curr_key != key)
$('body').append(String.fromCharCode(key));
}
else
$('body').append(String.fromCharCode(key));
}
}
$(function(){
$('html').live({
keydown:function(e){
handleKeys(e);
},
keypress:function(e){
handleKeys(e);
}
})
})
</script>
</head>
<body>12345</body>
</html>
I had the same issue with keypress on mozilla.
Thanks to this subject it solves my problem so I'll post my code if anyone try to do the same thing as me.
In my exemple I try to auto space when the user type two numbers, and it didn't work in Firefox so that's my code :
$(function() {
$('#field1, #field2').on('keypress',function(event) {
event = event || window.event;
var charCode = event.keyCode || event.which,
lgstring = $(this).val().length,
trimstring;
if(charCode === 8) {
event.returnValue = false;
if(event.preventDefault)
event.preventDefault();
if(0 < $(this).val().length) {
$(this).val($(this).val().slice(0,-1));
}
}
else if(((charCode > 31) && (charCode < 48 || charCode > 57)) || lgstring >= 14) {
event.returnValue = false;
if(event.preventDefault)
event.preventDefault();
}
else {
trimstring = $(this).val().replace(/ /g,"");
if((lgstring !== 0) && (trimstring.length % 2) === 0 ) {
$(this).val($(this).val() + ' ');
}
}
});
});
I noticed that Mozilla handle the backspace as a keypress where Chrome don't.
Sorry for my English I'm French
$('#id').keypress(function(e) {
if(e.charCode > 0 || e.keyCode === 8){
if(e.keyCode === 8){
return true;
}else if((e.charCode !== 0) && ((e.charCode > 57 && e.charCode < 65)){
return false;
}
}else if((e.keyCode !== 0) && ((e.keyCode > 57 && e.keyCode < 65)){
return false;
}
});