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

120
Views
Regex issue with JavaScript

Just like the title I have an issue with the following code

function checkPhone(phone){
    var reg = "/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im";
    var result=phone.match(reg);
    if (result) {
        console.log("(true)phone  "+phone);
        return (true)
    } else {
        console.log("(error)phone "+phone);

    }
}
<div class="form-group">
                <label >Numero di Telefono</label><br>
                <input type="text" id="phone" onchange="checkPhone(this.value)" placeholder="Insert phone number">
</div> 

Regardless of what I try to put in phone, it will result in "(error)phone "+phone. I have also tried it online and it works.

How can I fix the issue?

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

0

The problem is that you have quotes around the regular expression. Try this:

function checkPhone(phone){
    var reg = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im;
    var result=phone.match(reg);
    if (result) {
        console.log("(true)phone  "+phone);
        return (true)
    } else {
        console.log("(error)phone "+phone);

    }
}
<div class="form-group">
                <label >Numero di Telefono</label><br>
                <input type="text" id="phone" onchange="checkPhone(this.value)" placeholder="Insert phone number">
</div>

See Regular Expressions on MDN.

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!