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

168
Views
How can I pass a Regular Expression through a function?

I want to validate some fields passing a different pattern to a validation function.

send.addEventListener("click", function (event) {
  let pattern = /^[A-Za-zÁ-Úá-ú\s]{3,15}$/;
  let nameIsVal = regexValidator(pattern);

  if (nameIsVal) {
    return true;
  } else {
    event.preventDefault();
    return false;
  }
});

function regexValidator(pattern) {
  if (!pattern.test(this.value)) {
    return false;
  } else {
    return true;
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I am assuming that you are in a class. Make sure that the this keyword points to the correct instance in your event handler, either with the .bind() keyword or with an arrow function.

I would register the handler like this:

send.addEventListener('click', (event) => this.checkRegex(event));

Or if your environment doesn't support arrow functions, this should work as well:

send.addEventListener('click', this.checkRegex.bind(this, event));

Then I would add the methods to the class like this:

checkRegex(event) {
    let pattern = /^[A-Za-zÁ-Úá-ú\s]{3,15}$/;
    let nameIsVal = this.regexValidator(pattern);

    if (nameIsVal) {
        return true;
    } else {
        event.preventDefault();
        return false;
    }
}

regexValidator(pattern) {
    if (!pattern.test(this.value)) {
        return false;
    } else {
        return true;
    }
}
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!