Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
How do I create validation on multiple values using regex in Javascript node.js

Here's what I have tried:

let args = getArgs(message, commandRecd).split(",");
        if (args == validday && !validdays && !validhour && !validhours && !validminute && !validminutes ){
          countdown()
        }
        if (args == validdays && !validday && !validhour && !validhours && !validminute && !validminutes ){
          countdown()
        }
        if (args == validhour && !validday && !validdays && !validhours && !validminute && !validminutes ){
          countdown()
        }
        if (args == validhours && !validday && !validdays && !validhour && !validminute && !validminutes ){
          countdown()
        }
        if (args == validminute && !validday && !validdays && !validhour && !validhours && !validminutes ){
          countdown()
        }
        if (args == validminutes && !validday && !validdays && !validhour && !validhours && !validminute ){
          countdown()
        }

The validated regex is:

const validday = /\dday/
const validdays = /\dday/
const validhour = /\dhour/
const validhours = /\dhours/
const validminute =/\dminute/
const validminutes = /\dminutes/

But when I tried and made my request as 1minute, the countdown does not start.

p.s.: There's no problem with the countdown code, as the countdown code itself works when I run it without regex validation.

p.p.s.: using discord.js

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem here is that args is an array (args.split returns an array), and you are trying to validate args as a string. (args == validday, etc.) Since args is an array, and not a regex, the if/else statement returns false, thus your countdown function does not execute. On top of that, you shouldn't be using == to validate a string using regex, use .test instead for a true/false value.

On top of that, using multiple if else blocks with the same parameters isn't recommended as the more if else blocks you add, the harder it is to debug and change values, thus I changed this into a for..in loop to make it cleaner.

The following code is assuming that the 2nd element in args is the data you're looking for. You may need to edit this if needed.

I have rewritten your code here, with documentation on what each line means:

let args = getArgs(message, commandRecd).split(",");
//second element of args, change this as needed, this is the data that we are checking the regex against
var data = args[1] 

//These are the regexes that we are checking data  against
var validationArr = [/\dday/, /\dhour/, /\dhours/, /\dminute/, /\dminutes/]
//for every element in validationArr, we check it against the data to see if we get a match
var i = 0; //counts the number of times we get a match
for(x in validationArr){
   //.test returns true if there is a match
   if(validationArr[x].test(data)){
       i++;
   }
}
//this means that we have only gotten one match out of all the regexes.
if(i == 1){
   countdown()
}

Further reading on <Regex>.test() Hope this helps! Feel free to comment if you don't understand a line.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!