US Phone Number Verification

前端 未结 13 1782
眼角桃花
眼角桃花 2020-11-30 14:00

I have a website form that requires a US phone number input for follow up purposes, and this is very necessary in this case. I want try to eliminate users entering junk data

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 14:45

    This is a quick function that I use (below). I do have access to a zipcode database that contains areacode and prefix data which is updated monthly. I have often thought about doing a data dip to confirm that the prefix exists for the area code.

        public static bool isPhone(string phoneNum)
        {
            Regex rxPhone1, rxPhone2;
    
            rxPhone1 = new Regex(@"^\d{10,}$");
            rxPhone2 = new Regex(@"(\d)\1\1\1\1\1\1\1\1\1");
    
            if(phoneNum.Trim() == string.Empty)
                return false;
    
            if(phoneNum.Length != 10)
                return false;
    
            //Check to make sure the phone number has at least 10 digits
            if (!rxPhone1.IsMatch(phoneNum))
                return false;
    
            //Check for repeating characters (ex. 9999999999)
            if (rxPhone2.IsMatch(phoneNum))
                return false;
    
            //Make sure first digit is not 1 or zero
            if(phoneNum.Substring(0,1) == "1" || phoneNum.Substring(0,1) == "0")
                return false;
    
            return true;
    
        }
    

提交回复
热议问题