Checkbox status changing onkeypress - javascript

I am using the JavaScript to disable keyboard if checkbox is not checked and to enable only numbers if checkbox is checked.
My code is.
<script type="text/javascript">
function isNumberKey(evt)
{
if(document.getElementById("check1").checked=false)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
else
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 1 && charCode < 127)
return false;
return true;
}
}
</script>
HTML code
<asp:CheckBox ID="check1" runat="server"/>
<asp:TextBox ID="tbxt1" runat="server" MaxLength="6" Width="200" onkeypress="return isNumberKey(event);" TextMode="Password" autocomplete="off" />
In default as checkbox is not checked so JavaScript is not allowing me to use my keyboard. This is fine. When I change my checkbox status to checked and tried pressing a key. OnKeyPress event the checkbox status is changing to unchecked. Is there anything wrong in my code?

You have a bug in your code, you need double equlas == to check an equality in javascript.
Like this:
if(document.getElementById("check1").checked==false)
Also, it can be very confusing to somebody else to read your code if you don't contain all conditional stuff within a pair of curly braces, even when it's a single line.
So I'd refactor it ever so slightly to be like this (just for clarity, I haven't really changed anything):
<script type="text/javascript">
function isNumberKey(evt)
{
if(document.getElementById("check1").checked==false)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
return false;
}
else
{
return true;
}
}
else
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 1 && charCode < 127)
{
return false;
}
else
{
return true;
}
}
}
</script>

Related

Need Number validation regular expression

The following point must be considered before creating a regular expression:
Add +(plus) before Every number that user cant delete.
number length min 2 max 20
only number,backspace, Arrowkey allow
Right now I'm using this:
function Validatenumber(event) {
var regex = new RegExp("^[0-9?.*+*]+$");
var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
if (regex.test(key) || event.keyCode == 8) {
return true;
}
else{
event.preventDefault();
return false;
}
}
Try this(Got this from my project).
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Try below code
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1 /jquery.min.js"></script>
<script>
function isNumber(evt) {
evt = (evt) ? evt : window.evt;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 37 && charCode != 39) {
return false;
}
return true;
}
</script>
</head>
<body>
Enter your name: <input type="text" onkeypress="return isNumber(event);">
</body>
</html>
I got Right Answer , i know i m fresher and and i don't have enough knowledge but if u have than share with any one instead of finding mistake in Question , why u use this tag and blah blah ...
https://github.com/stripe/jquery.mobilePhoneNumber
Check out this link for perfect answer of my question...
and thank You very much friends who answer my question or try to answer my question .. keep it up friends...

Javascript currency regular expression replace

For my business web application I want a user to only be able to enter valid currency values in a textbox.
Currently I use
$input.val($input.val().replace(/[^.\d]/g, ''));
But this doesn't take in consideration order or multiple decimal seperators.
So the user either has to enter a whole integer or a valid decimal value e.g.:
49
49.50
Bonus points if this is allowed too:
.50 (for 0.50)
So I don't want to validate, I want to restrict typing into the textbox. Is this possible with a single regexp replace?
We can do this in two steps. Restrict the user to type only the number characters and . character.
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode < 31 && (charCode > 48 || charCode < 57))
return true;
return false;
And the next one is for allowing the .:
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 46 && (charCode > 31 && (charCode < 48 || charCode > 57)))
return true;
return false;
The next step will be not allowing double periods.
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 46 && (charCode > 31 && (charCode < 48 || charCode > 57)))
if (charCode == 46 && value.indexOf(".") !== false)
return true;
return false;
Hope you can have this as a starting point.
Snippet
Open Deal: Break it if you can.
function check(inp) {
var charCode = (event.which) ? event.which : event.keyCode;
console.log(charCode);
if (charCode == 8)
return true;
if ((charCode > 46 && charCode < 58) || (charCode > 95 && charCode < 106) || charCode == 110 || charCode == 190)
if (charCode == 110 || charCode == 190)
if (inp.value.indexOf(".") == -1)
return true;
else
return false;
else
return true;
return false;
}
<input type="text" onkeydown="return check(this);" />
It's more user friendly to advise of errors and let users fix them themselves. You might consider using the keyup event, but showing an error too early (before the user has had a chance to enter a valid value) can be annoying too. e.g.
function validate(el) {
var type = el.className;
var errEl = document.getElementById(el.name + 'Err');
if (type == 'typeCurrency') {
var currencyRe = /^-?(\d+(\.\d{1,2})?|\.\d{1,2})$/;
errEl.style.visibility = currencyRe.test(el.value)? 'hidden' : 'visible';
}
}
.errorMessage {
color: red;
background-color: white;
visibility: hidden;
}
Cost <input onblur="validate(this)" name="foo" class="typeCurrency" placeholder="0.00"><br>
<span id="fooErr" class="errorMessage">Please enter a valid cost, e.g. 2.45</span>

Check Integer Validation on texbox using Javascript

I have tried with below Keycode but its not working.
HTML:
<asp:TextBox ID="txtDays" runat="server" TabIndex="1" autocomplete="off" CssClass="form-control"
placeholder=" " onkeypress="return integerValidation(this)" onblur="ShowMaterial('', 'BindGridview')" onfocus="return validateOnFoucs(this)"
Text="60" MaxLength="3"></asp:TextBox>
Js:
function integerValidation(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 100 && (charCode < 58 || charCode > 90)) {
return false;
}
else {
return true;
}
}
Use isNaN with Number or parseInt
isNaN Will return true if the value isn't a number and false if it is(Is Not A Number).
Some this like this:
function integerValidation(input)
{
return !isNaN(Number(input.value));
}
Or:
function integerValidation(input)
{
return !parseInt(input.value);
}
Your code is not correct.
return integerValidation(this)
Use this instead.
return integerValidation(event)
and your logic seems wrong
if (charCode > 100 && (charCode < 58 || charCode > 90))

Allow user to paste numeric values either by ctr+c ctr+v OR by right click copy paste

I want to copy paste numeric values in this field, can we achieve this keeping onkeypress as it is?
HTML:
<input type="text" onkeypress="return allowNumOnly(event);">
Javascript:
function allowNumOnly(evt)
{
var charCode = (evt.which) ? evt.which : evt.keyCode
console.log(charCode);
if(charCode==36 || charCode==46 || charCode==37 || charCode==39)
return true;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}

how to define event below

I have a function below where it doesn't allow the user to type in letters in a textbox but it keeps saying event is undefined. Where and how am I suppose to define event?
Below is the function:
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Below is the html where this could be used:
onkeypress="return isNumberKey(event)"
change
var charCode = (evt.which) ? evt.which : event.keyCode
to
var charCode = (evt.which) ? evt.which : evt.keyCode
or if you are using jQuery (you tagged it but aren't using it) you could do
$('#textareaid').keypress(function(e) {
if (e.which > 31 && (e.which < 48 || e.which > 57)) {
e.preventDefault();
}
});
where your html is
<textarea id="textareaid"></textarea>
See event.preventDefault()
Note on how to get key pressed using jQuery
To determine which character was entered, examine the event object
that is passed to the handler function. While browsers use differing
properties to store this information, jQuery normalizes the .which
property so you can reliably use it to retrieve the character code.
Taken from the .keypress() docs
here is the code which should work:
function checkerFunction(e){
var charCode=e.which || e.keyCode;
if(charCode<=31 || charCode>=48 && charCode<=57)return true;
return false;
}
and the event binding should be
onkeypress="return checkerFunction(window.event)"
For the record I think that most of the answers above don't take into consideration the ARROW keys.
This should be how it works incase you are googling this.
function isNumberKey(event)
{
var charCode = event.charCode;
if ((charCode < 48 || charCode > 57) && charCode != 0){
return false;
}
return true;
}

Categories