Search code examples
asp.netvb.netwebformscustomvalidator

Custom Validator on phone number and further information on it


I'm looking for a good site that explains CustomValidator in detail. If anyone has a go to please let me know. The following code checks that there are at least 10 numbers in the result. However, I'm looking to also have it validate that the values are numbers. Using CustomValidator in vb.net is there a way to do this as an "and if" statement?

Thank you

Sub AtLeastTenNumbers_ServerValidate(ByVal source As Object, _
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

    If area_code.Text.Length + phone_1.Text.Length + phone_2.Text.Length > 9 Then
        args.IsValid = True
    Else
        args.IsValid = False
    End If

End Sub

Solution

  • This is a bit of an old article, but Scott Mitchell knows his stuff:

    https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx

    is there a way to do this as an "and if" statement?

    You can always nest an if statement inside of your current statement.

    If area_code.Text.Length + phone_1.Text.Length + phone_2.Text.Length > 9 Then 
        args.IsValid = True 
        'Check to see if this part is numeric
        If IsNumeric(phone_1.Text) Then
           ' Do Logic here
        End If
    Else 
        args.IsValid = False 
    End If