How can i set more than one id in one function - javascript

i have 2 function in jquery like this :-
$('#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
$('#phone').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
this tow function is same to do .
so i need to mearge this tow function in one like :-
$('#phone') + $('#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});

like this: just for demo sake: http://jsfiddle.net/2ASnz/
You can combine multiple selectors with a comma.
This link will give you more insight: http://api.jquery.com/multiple-selector/
Hope this help :)
code
$('#phone,#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
http://api.jquery.com/multiple-selector/
You can specify any number of selectors to combine into a single
result. This multiple expression combinator is an efficient way to
select disparate elements. The order of the DOM elements in the
returned jQuery object may not be identical, as they will be in
document order. An alternative to this combinator is the .add()
method.

try this
$('#phone,#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});

Give the two elements same class and select them by their class, then do same thing to both elements.

jQuery generally works on collections. You can pass more than one selector expression separated by commas. This will work as you required.
$('#phone, #age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});

You can just use , in your selectors to have multiple selectors Multiple Selectors
$('#phone,#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});

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 prevent non numeric Character and accepts 3 decimal places

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;
}

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));
}

text field that only allow and number and dash and also accepts ctrl commands

function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode != 45 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
i have been using this function but its does not allow ctrl + commands.
like ctrl + a , ctrl + c
May be you want to do something like this to sort it out:
function isNumberKey(evt) {
var charCode = evt.which || event.keyCode;
if (!evt.ctrlKey && charCode != 45 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
just check if ctrlKey is pressed, if do then only in conjunction with !evt.ctrlKey disable it.

Categories

Resources