Is there an easier way to make many inputs equal to one?

Ok, so at the moment I'm writing a script where you need to specify how often something happens, and the script I used was "Monthly" to mean once a month. But obviously, people can do more than just "monthly".

So, I wrote this just in case they write something else.

if (RepeatDay === "once a month" || RepeatDay === "Once a Month" || RepeatDay === "every month" || RepeatDay === "month" || RepeatDay === "Month" || RepeatDay === "monthly"|| RepeatDay === "Once A Month"){
    var RepeatDay = "Monthly"
}

      

I have many of these types of inputs and I was wondering if there is an easier way to do it.

+3


source to share


4 answers


You can create an array

var arr=["once a month","Once a Month","every month","month" ,"Month","monthly","Once A Month"]

if(arr.indexOf(RepeatDay)!==-1){
     RepeatDay='monthly';
}

      



NOTE : indexOf doesn't work in IE <9, so you need to do it the classic way

for(var i=0;i<arr.length;i++){
    if(arr[i]===RepeatDay){
        RepeatDay="monthly";
        break;   //no need to check for further after a match is found
    }
}

      

-2


source


you can use

if(RepeatDay.toLowerCase().indexof('month')>-1){
    var RepeatDay = "Monthly";
}

      



But as suggested in the comments, it would be better to specify the dropdown user instead of text input in a scenario like this

+3


source


As stated, I highly recommend allowing the user to choose from a set of options that you control.

If you really want to check a list like this, you have several options (all most likely combined with toLowerCase

to reduce the number of things you have to check):

  • You can use an array as Ankit demonstrated .

  • You can use an operator switch

    that also makes it easy to match other values ​​such as "weekly", etc.

    switch (RepeatDay.toLowerCase()) {
        case "once a month";
        case "every month";
        case "month";
        case "monthly":
            RepeatDay = "Monthly"
            break;
        case "weekly":
        case "once a week":
            RepeatDay = "Weekly";
            break;
        // ...
    }
    
          

  • You can use the object as a map, which also makes it easy to display "weekly" and so on:

    var repeatOptions = {
        "once a month": "Monthly",
        "every month": "Monthly",
        "month": "Monthly",
        "monthly": "Monthly",
        "weekly": "Weekly",
        "once a week": "Weekly",
        // ...
    };
    
          

    ... then:

    RepeatDay = repeatOptions[RepeatDay.toLowerCase()];
    if (!RepeatDay) {
        // Invalid option specified
    }
    
          

+1


source


I recommend that you think differently about collecting user input.

A drop-down list with parameters will be more useful for you and your users.

Other answers may work. But consider that the user still needs to enter values ​​that need to be added to add certain words for the input to work as expected. You will end up with a lot of unnecessary explanations of messages or checks in your code.

Don't know your exact suggestion, but the dropdown to choose between the type of interval (monthly, weekly, yearly, daily) and the type number input anant to know the number of times that happens during this type of interval should work and be more intuitive.

For mi, these are valid values ​​that you need to take care of.

(x random number of times)

xper month

ht

x per month

x times

x M

Every tenth

Once m

First million of each m

Only in June

Etc...

You don't need a thousand-case array, and obviously you cannot confirm that the word month or word m exists (see exaples again).

+1


source







All Articles