使用Google的libphonenumber库接受移动电话,但不接受固定电话号码

问题描述 投票:-2回答:1

我正在使用Google的libphonenumber库来验证电话号码,但是我只接受手机号码而拒绝固定电话号码。

这可能吗?

这是我的电话号码验证码:

Result PhoneContentValidator(string phoneNumber, string region)
{
     Result result;
     PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
     PhoneNumber phone = phoneUtil.Parse(phoneNumber, region.ToUpper());

     try
     {
         if (phoneUtil.IsValidNumber(phone) != true) return result = new Result { resultIsValid = true, resultText = "Not Valid Mobile Number", result = false };
         else return result = new Result { resultIsValid = true, resultText = Main_Log.ApplicationSuccessMessage, result = true };
     }
     catch (Exception ex)
     {
          Main_Log.GetLogger().Error(Main_Log.MessageForLogFile("phone content validation failed due to the exception in application. ", ex.Message, ex.HResult));
           return result = new Result { resultIsValid = false, resultText = ex.Message };
     }
}

此方法同时验证固定电话和移动电话号码。我要拒绝固定电话号码并接受手机号码。

c# libphonenumber
1个回答
2
投票

最后我发现这是为了只接受手机号码:

if (phoneUtil.GetNumberType(mobile) == PhoneNumberType.MOBILE) return true
© www.soinside.com 2019 - 2024. All rights reserved.