I have this function that only allows number in keypress how can i change this function ou method to call, so when i do ctrl+v verify too the values?
function isNumberKey(evt, obj) {
var containsDot = obj.value.indexOf(".");
var nrDecimals = Decimals(obj.value, ".");
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode == 8 || charCode == 9) {
return true;
} else if (charCode > 31 && (charCode < 48 || charCode > 57)) {
if (charCode == 46 && containsDot < 0) {
return true;
}
return false;
}
if (nrDecimals > 2) {
return false;
}
return true;
}
I call it this way
onkeypress="return isNumberKey(event,this);"
If you only want numbers in your input, you could do something like this:
onkeyup="this.value = this.value.replace(/[^0-9]+/, '');"
This replaces all non-number characters from the value. This does about the same as your whole function does, plus it works with the paste.
You need to change onkeypress to onkeyup.
For the full solution you need to switch to more complex code. As one of the comment below says, you can rightclick->paste aswell. This will not catch that. You need to bind multiple event (keypress, keyup, click are examples of events) to that one input. If you're lucky, focus will catch it (if rightclick blurs it, and after paste it focusses).
you can use jQuery. Change the below code as per your requirement.
$(document).ready(function() {
$("#id")
.bind('copy', function(e) {
alert('copying!');
})
.bind('paste', function(e) {
alert('pasting!');
})
.bind('cut', function(e) {
alert('cut!');
});
});
If you want to use plane javascript. I think below will work, but not sure about the browser compatibility.
<input type="text" onpaste="callFunc()" />
Related
hi i have this function to allow digits 0-9 but i want to include the negative sign. how would i do that? Thanks.
function allowNumbersOnly(e) {
var code = (e.which) ? e.which : e.keyCode;
if (code > 31 &&(code < 48 || code > 57)) {
e.preventDefault();}
else if (code==109){
e.preventDefault();
}
}
Also see HTML text input allow only numeric input.
Add a test to check if it's the first character and allow "-". Otherwise, run the other tests. e.g.
function allowNumbersOnly(e) {
let len = e.target.value.length;
var code = (e.which) ? e.which : e.keyCode;
if (len == 0 && code == 45) {
return;
}
if (code > 31 && (code < 48 || code > 57)) {
e.preventDefault();
} else if (code == 109) {
e.preventDefault();
}
}
window.onload = function() {
document.querySelector('#i0').addEventListener('keypress', allowNumbersOnly, false);
}
<input id="i0">
However, that only allows entry of "-" as the first and only character, it can't be added later to change say "1" to "-1".
Personally, I find this kind of UI feature very annoying, e.g. it prevents keystrokes to copy and paste values. Just let the user enter whatever they like and deal with it. If it doesn't fit the required criteria, just let the user know with a friendly message and let them fix it.
onkeypress is not working with textbox in asp.net webform
I want to prevent special characters and alphabets
Following is the aspx
<wijmo:C1InputText ID="SAC" runat="server" Style="width: 150px; display: inline-block;" onkeydown="return onlyNumbers(this,event);">
</wijmo:C1InputText>
function onlyNumbers(txt, event) {
var charCode = (event.which) ? event.which : event.keyCode
if (charCode == 46) {
if (txt.value.indexOf(".") < 0)
return true;
else
return false;
}
var regex = new RegExp("^[a-zA-Z0-9]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=190)
return false;
return true;
}
It returns false but not preventing to enter.
If I replaced onkeypress with onkeydown it works
Please have a look on this post :
https://forums.asp.net/t/1888076.aspx?onKeyPress+vs+OnKeyDown+vs+OnKeyUp
this is the part of the answer you want :
OnKeyDown allows you to fire an event prior to the character being entered within the Textbox, so that you could check what the character being pressed is prior to determining if it should be allowed or not.
OnKeyUp will allow you to essentially hold a key down and will not fire an event until the key is released.
OnKeyPress will fire an event for each character key that is pressed (including those being held down).
I'm having problems avoiding the special chars (Alt + number from Bloq. num -> ☺☻♥♦♣♠•◘○)in an input.
My input is declared like this:
<html:text styleId="inputMaxVol" styleClass="numeric"/>
This is on my onReady
$(".numeric").keydown(function(event) {
return maskNumber();
});
And here is where I'm trying to do the job (with no success)
function maskNumber() {
if (event) {
var charCode = (event.which)? event.which : event.keyCode;
console.log(charCode);
// If not a number
if ((charCode < 47 || charCode > 58) && (charCode < 95 || charCode > 105)) {
console.log("not a number");
return false;
} else if (event.altKey) {
//No special characters admitted
console.log("Alt pressed");
return false;
}
} else {
return false;
}
}
If I press Alt + 1 (key "1" from the numeric pad) the white smiley face appears and in my log appears something like that
18
not a number
97
Alt pressed
I've tried too with the event.preventDefault() with no success. Any idea about where is my problem?
Thanks, I really appreciate any help you can provide.
UPDATE: As explained in comments the solution comes changing the keydown event for keypress and changing the charcode comparation numbers (Now is only if (charCode < 48 || charCode > 57) due to the numbers returned have changed too.
So the solution is as follows
$(".numeric").keypress(function(event) {
return maskNumber();
});
function maskNumber() {
if (event) {
var charCode = (event.which)? event.which : event.keyCode;
// If not a number
if (charCode < 48 || charCode > 57) {
return false;
}
} else {
return false;
}
}
Simple as that!
Try below code it will work on each browser or if you want to use library then visit this link: https://github.com/tzuryby/jquery.hotkeys
$("#txtmy").keypress(function(event) {
if (event.which.toString().length > 3) {
event.preventDefault();
return;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtmy"/>
Returning false will not disable the event.
You need to do an event.preventDefault(), which stops the event from happening, instead of returning false.
event.preventDefault();
The return false is working for an onbeforesubmit event, of a Form.
Hi i have a function which accepts numbers but there's a few problems with it.
1) i want it to accept slashes i.e. '/'?
2) On the first input it accepts letters for some reason.
it binded to a knockout keyup function.
<input id="txtboxToFilter" type="text" placeholder="dd/mm/yyyy" maxlength="10" data-bind="value: Observable.birthdate(), valueUpdate: 'keyup', event: { keyup: CheckDate}" />
Which then calls this function.
function CheckDate(){
document.getElementById('txtboxToFilter').onkeydown = function(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
}
Any body help?
You can certainly use Regex:
here is one which will support leap years as well :)
^(?:(?:31(\/)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
JS code:
document.getElementById('txtboxToFilter').onblur= function(e) {
alert(validateDate(document.getElementById('txtboxToFilter').value));
}
function validateDate(dob) {
var re = /^(?:(?:31(\/)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;
return re.test(dob);
}
fiddle: http://jsfiddle.net/exe0m3ek/
NOTE: I have changed the event from keyup to blur, If you only want
this on keyup...please change accordingly.
Looking for a bit of help to modify some already-working JavaScript.
I am using a piece of JavaScript along with a filtered textbox, the filtered text box only allows {1234567890,-}
The below JavaScript works and prevents the user from entering more than 1 decimal
I want to also add a dash/minus {-} to the allowed characters and prevent duplicates
<script type="text/javascript">
function PreventDuplicateDecimal(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var itemdecimal = evt.srcElement.value.split('.');
if (itemdecimal.length > 1 && charCode == 46)
return false;
return true;
}
</script>
I have tried adding;
I have tried adding
var itemdash = evt.srcElement.value.split('-');
and adding it as a 'or' statement but no luck
Any help would be much appreciated
Thanks in advance
Paul
if(evt.srcElement.value.indexOf(".") > -1) {
if (charCode === 46) {
return false;
}
}
if(evt.srcElement.value.indexOf("-") > -1) {
if (charCode === 45) {
return false;
}
}
return true;