How to restrict user to check only 5 check boxes [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have more than 50 options but the user must select a maximum of 5 check boxes.
How could I restrict the user to select up to 5 check boxes?

Use a common if statement like so:
if ($('input[type="checkbox"]:checked').length > 5) {
alert('exceed 5')
}

Try $('input:checkbox:checked').length
Example
var total = $('input:checkbox:checked').length;
if (total <= 5) {
return true;
} else {
return false;
}

Related

what is the work of .id in this given function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 hours ago.
Improve this question
what is .id in this given function
function getSelected() {
let answer = undefined;
answerEls.forEach((answerEl) => {
if (answerEl.checked) {
answer = answerEl.id;
}
});
return answer;
}
I tried to find what is .id by console .log but it does does not show any value

Javascript false control [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a problem I want the script not to run unless I am less than 5 trainqueue_barracks
but I put 100 here and it doesn't work either. What's wrong?
if ( document.document.getElementById("trainqueue_barracks") == null
|| document.document.getElementById("trainqueue_barracks").rows.length < 5
) {
As you have tagged jquery, you can simply do it like this:
if(!$('#trainqeueu_barracks').length || $('#trainqeueu_barracks tr').length < 5)
{
...
}
Note that !$('#trainqeueu_barracks').length will be always true in your case, so the second part won't actually matter. I think what you really need is:
if($('#trainqeueu_barracks tr').length < 5)
{
...
}
First off, you shouldn't need the first condition of your loop
document.document.getElementById("trainqeueu_barracks") == null
Also if you put 100 in their then it shouldn't work and your code is right. Unless you want it to run when rows.length is >= 5.
document.document.getElementById("trainqeueu_barracks").row.length >= 5
Is the opposite end of what you have.

How to filter value using javascript? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have attached the image:
From the above image, I have to filter such data "1565158223" and "4" at any place of receiver_id_1, receiver_id_2, sender_id but together.
I want to remove other data which contains "3","1", ect.
I want a solution with minimal code and minimum loops as there are too many records i get it.
I can check the condition like (receiver_id_1 == "1565158223" AND "sender_id" == "4") OR (receiver_id_2 == "1565158223" AND "sender_id" == "4") OR (receiver_id_2 == "1565158223" AND "receiver_id_1 " == "4") Like wise..
But I dont want to write several conditions over here.
Please anybody help me to resolve it.
If you can use the features of es6 try this(we suppose your array is items):
filtered_items = items.filter(item =>
[1565158223,4].indexOf(parseInt(item['receiver_id_1'])) > -1 &&
[1565158223, 4].indexOf(parseInt(item['receiver_id_2'])) > -1 &&
[1565158223, 4].indexOf(parseInt(item['sender_id'])) > -1
)
Notice that it won't change the origin items and just filtered_items is your final filtered items.

Using if statement inside if statement, good or bad practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is it OK to use if statement in this form, or should i need to use something else to get result. I want to get effect if filtration is on 100 % then check cX if is on 100 % to add sign-ok not before filtration is on 100 %.
Just need opinion is it a good coding or I should change the approach?
if (filtration === 100) {
$(".filtration").removeClass('sign-no');
$(".filtration").addClass('sign-ok');
if (cX === 100){
$(".cX").removeClass('sign-no');
$(".cX").addClass('sign-ok');
if (stripping === 100) {
$(".stripping").removeClass('sign-no');
$(".stripping").addClass('sign-ok');
if (precipitation === 100) {
$(".precipitation").removeClass('sign-no');
$(".precipitation").addClass('sign-ok');
}
}
}
}
No issues. You can use. But if only first condition is true, then second condition will be checked and if it is true only it check the third condition and likewise.
Its okay, you can do it. But if you set a value on code quality, you should use maximal 2 depth in a block.
Must Code Quality Tools has a configuration for it.
ESLint
JSLint
....

Javascript form validation pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to validate that two characters and a number were correctly input.
var studentValid = /^[MTWTF][AL][1-9]$/i;
if (studentValid.test(studentTemp.value))
{
alert("true");
}
else
{
alert("false");
}
Yet everything I enter turns out false?
The problem is with your regexp (/^[MTWTF][AL][1-9]$/i). What this tells you is that you first want one of the characters M,T,W,T or F, and after that either A or L and finaly a number (and nothing before or after this).
So for example
ML4, WA5, FL9
will give you true
while
AM9, ML0, MMA5, MA99
will give you false.
Is this the pattern you are trying to match? There is nothing else wrong with your code and a valid value will give you true, for example:
var studentValid = /^[MTWTF][AL][1-9]$/i;
var value = 'MA9';
if (studentValid.test(value))
{
alert("true");
}
else
{
alert("false");
}
When working with regexp, it can be very usefull to use a tool to help you build it, check out https://regex101.com/r/A5FOIh/3 where you can try your different studentTemp.value to see if they match.

Categories

Resources