How to prevent non numeric Character and accepts 3 decimal places - javascript

I have this code it's working but it's accept small letter (e) and big (E)
How can I prevent that? I've seen some similar codes that also accepts (e & E)
function isNumberKeyDecimal(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
return false;
return true;
}

Related

Javascript Regex Allow One Decimal

javascript code
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
this works well for the numbers. but it adds multiple decimals.i just want it to allow only one decimal. any sort of help will be appreciated. thanks in advance

How to allow only numbers in textbox

function AllowOnlyNumbers(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)){
if (charCode === 8 && charCode === 46) {
return false;
}
}
return true;
}
How to allow only numbers and delete key or backspace to be written in this textbox ?
Why not use input of type number <input type="number" /> for browsers that supports it, otherwise use javascript:
function AllowOnlyNumbers(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;
}
Here is a fiddle: http://jsfiddle.net/Lm2hS/
From: https://stackoverflow.com/a/7295864/235659
I have created this solution for your problem here:
https://codebrace.com/editor/b05f92054
Here I have used event.charCode == 0 to allow non characters key pressed (To allow delete, backspace and other non-character keys) and isNaN to check if the value entered is a number or not.
I hope this helps!

How to apply conditional return statement in JavaScript

I have this html input:
Rows: <input type="text" class="rows" onkeypress="return isNumber(event)"><br>
and this javascript function to validate only numbers
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;
}
but i want to work with micro-branching to do something like this:
function isNumber(evt){
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
(charCode > 31 && (charCode < 48 || charCode > 57)) && return false;
return true;
}
the thing is that the last 2 lines didnt work.
return is a statement rather than an expression, and thus cannot be used as argument to a logical operator.
In your case however, the last two lines can be rewritten into a single return statement, by simply inverting the condition to the if clause:
return !(charCode > 31 && (charCode < 48 || charCode > 57));
Or, as zerkms notes, you can lose the ! by flipping the operators (&& <=> || and < <=> >=), which, in my humble opinion, increases readability:
return charCode <= 31 || (charCode >= 48 && charCode <= 57);
According to your description, it looks like you are looking for conditional check and return :
function isNumber(evt){
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
return !(charCode > 31 && (charCode < 48 || charCode > 57));
}

Why my code doesn't allow specific symbols?

I am using this code to allow only digits to type in textbox but now I want to allow . too. I modified this code but not working.
function isNumberKeyDotAllowed(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode == 46)
return false;
return true;
}
TextBox declaration in markup:
<asp:TextBox runat="server" ID="txtBoxApplicantCNICNo"
onkeypress="return isNumberKeyDotAllowed(this)" AutoPostBack=True
OnTextChanged="txtCHan_event" CssClass="form-control">
I see two problems with your code. The first is that you pass this as the argument to isNumberKeyDotAllowed while you should pass event:
onkeypress="return isNumberKeyDotAllowed(event);"
The second is the validation condition. Here is my own version of the function. I defined the condition for success instead of the condition for failure, because it is easier for me to figure out:
function isNumberKeyDotAllowed(evt) {
var charCode = evt.which ? evt.which : evt.keyCode;
if (charCode == 46 || (48 <= charCode && charCode <= 57)) {
return true;
}
else {
return false;
}
}

Javascript validation won't allow periods/decimal points

I'm having some trouble adding validation for stops to my input box. The restriction on allowing numbers only (second condition) works, but the first condition may as well not exist. Full stops and decimal points still do not appear in the input box.
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 110 || charCode == 190)
return true;
else if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Additionally, I've noticed that the behaviour of this JS is different across browsers. in FireFox, I can use the numeric keypad to enter a value. However, in Chrome I am limited to the top row of numbers. Neither browser allows decimal points.
onkeypress won't report the key codes, it will report the ASCII character typed. You want onkeydown
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 110 || charCode == 190)
return true;
else if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
document.getElementById('foo').onkeydown = isNumberKey;
<input type="text" id="foo" />

Categories

Resources