US Phone Number Verification

前端 未结 13 1665
眼角桃花
眼角桃花 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条回答
  • 2020-11-30 14:51

    Those parameters look pretty good to me, I might also avoid numbers starting with 911 just to be safe.

    0 讨论(0)
  • 2020-11-30 14:52

    If you're sticking with just US- and Canada-format numbers, I think the following regex might work: [2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9] & ![2-9][0-9][0-9]-555-[0-9][0-9][0-9][0-9]

    0 讨论(0)
  • 2020-11-30 14:52

    I don't nkow if this is the right place, it's a formatting function rather than a validation function, I thought let's share it with the community, maybe one day it will be helpful..

    Private Sub OnNumberChanged()
        Dim sep = "-"
        Dim num As String = Number.ToCharArray.Where(Function(c) Char.IsDigit(c)) _
                                                     .ToArray
        Dim ext As String = Nothing
        If num.Length > 10 Then ext = num.Substring(10)
        ext = If(IsNullOrEmpty(ext), "", " x" & ext)
        _Number = Left(num, 3) & sep & Mid(num, 4, 3) & sep & Mid(num, 7, 4) & ext
    End Sub
    

    My validation function is like so:

    Public Shared Function ValidatePhoneNumber(ByVal number As String)
        Return number IsNot Nothing AndAlso number.ToCharArray. _
                                      Where(Function(c) Char.IsNumber(c)).Count >= 10
    End Function
    

    I call this last function @ the OnNumberChanging(number As String) method of the entity.

    0 讨论(0)
  • 2020-11-30 14:54

    867-5309 is a valid phone number that is assigned to people in different area codes.

    0 讨论(0)
  • 2020-11-30 14:56

    In Django there is a nice little contrib package called localflavor wich has a lot of country specific validation code, for example postal codes or phone numbers. You can look in the source too see how django handles these for the country you would like to use; For example: US Form validation. This can be a great recourse for information about countries you know little of as well.

    0 讨论(0)
  • 2020-11-30 14:56

    For US and International Phone validation I found this code the most suitable:

    ((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$
    

    You can find an (albeit somewhat dated) discussion here.

    0 讨论(0)
提交回复
热议问题