Validating in visual basic


21-Feb-2017 18:14

If you don't have either one, you have an error and bail out immediately.For seven-digit phone numbers, we prepend the default area code, and then build the final number with a little bit of string manipulation.Next, the code parses out the phone number and the extension. The phone system is such that you can't have a 1 as the first number of either an area code or a phone number prefix.

They let you verify that the whole application, including its user interface, is functioning correctly.He is certified in Visual Basic 5.0 and Windows Architecture (1 and 2). The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.Numbers entered without area codes will have a default area code prepended to them. Any extensions will be listed as xnnnnn following the phone number. If an initial 1 is added to the number, it will be removed, since users already know to dial a one before making a long distance call.

With these rules in mind, let's create the validation code for a text box named txt Phone: Private Sub txt Phone_Validate(Cancel As Boolean) Dim str Temp As String Dim str Phone As String Dim str Extension As String Dim int Result As Integer Const Default Area Code = "703" ' ' Remove all the grouping characters for ' now. ' str Temp = Replace(txt Phone, "(", "") str Temp = Replace(str Temp, ")", "") str Temp = Replace(str Temp, "-", "") str Temp = Replace(str Temp, " ", "") str Temp = Replace(str Temp, "X", "x") ' ' Break up the digits into the number and ' the extension, if any.' int Result = In Str(1, str Temp, "x", vb Text Compare) If int Result 0 Then str Extension = Mid(str Temp, int Result 1) str Phone = Left(str Temp, int Result - 1) Else str Phone = str Temp End If If Left(str Phone, 1) = "1" Then str Phone = Mid(str Phone, 2) End If If Len(str Phone) 10 Then Msg Box "Please enter a valid telephone number.", vb Exclamation Cancel = True Exit Sub End If ' ' Prepend the default area code ' If Len(str Phone) = 7 Then str Phone = Default Area Code & str Phone End If ' ' Build the new phone number ' txt Phone = "(" & Left(str Phone, 3) & ") " _ & Mid(str Phone, 4, 3) & "-" _ & Right(str Phone, 4) ' ' Add the extension, if any ' If str Extension I used a default area code of 703, which you can obviously change as needed.If you're going to be using the application in different locations, you may want to store that information with the application user's profile, similar to the way that Windows stores your favorite locations for dialing.Coded UI Tests are particularly useful where there is validation or other logic in the user interface, for example in a web page.