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

152
Views
JavaScript phone number validation not throwing error for more than 10 digit

I implemented a logic that checks whether the phone number is valid. However, it's not checking if the number should be only 10 digits.

self.validateContactInfo = function () {
  let messages = [];
  let regex = '^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$';
  let phone = self.phone.peek();
  
  if (!phome && !phone.match(regex)) {
      messages.push("Please enter valid phone number");
   }
   
   if (messages.length != 0) {
        puDialogV2.showDialog($('.body-content'), "Error", messages, self, function () { return; });
        throw new Error(messages);
    }
  
}

 <div class="contactItem">\
                    <label style="padding:6px" >Home phone</label>\
                    <input class="form-control" type="text" data-bind="textInput: Phone" /> \
                    <div class="contactSms" data-bind="if: smsNotificationFeatureEnabled, css: { hasInfoPopup: !smsNotificationFeatureEnabled() }">\
                    </div>\
                </div>\

With the above code, if I leave the phone number field black or enter any alphabetic character, it throws an error. But If I enter 12 digit number it does not. For example, if I enter 71356878901212, it does not throw any error

about 4 years ago · Juan Pablo Isaza
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!