yeojz / otplib

:key: One Time Password (OTP) / 2FA for Node.js and Browser - Supports HOTP, TOTP and Google Authenticator
https://otplib.yeojz.dev
MIT License
1.97k stars 132 forks source link

How to Validate Phone Number #607

Open indumathymk opened 3 years ago

indumathymk commented 3 years ago

Is there is an option to validate given phone number is Mobile or Landline?

sayertindall commented 3 years ago

No, you need to use a service like Twilio verify or something similiar.

reachbharathan commented 3 years ago

This cannot be verified by this repo, maybe it need to be validated via paid service.

r2ast commented 3 years ago

You can build a cx function that take the number entered in the input field. Then the number is used an argument which goes to a function and check length and pattern of the number entered. If the patterns has mobile length, consider as mobile else consider it a landline. This is how i have made. I used a single to input to identlify that entered input is a email or phone. Something like this.

validate_input1(email, phone) {
    var intRegex = /[0-9 -()+]+$/;

    if (intRegex.test(phone)) {
      // console.log("is phone", email);
      var obj1 = {
        mobile: phone,
      };

      if (phone.length < 9 || !intRegex.test(phone)) {
        alert("Please enter a valid phone number.");
        //return false;
      }
      return obj1;
    }
    if (email) {
      var eml = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
      // console.log("is email enterted", email);
      var obj2 = {
        email: email,
      };
      // console.log('get obj',obj);
      if (eml.test(email) == false) {
        alert("Please enter valid email address.");
      }

      return obj2;
    }
  }