Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda