Validating email format
In fact, that setup is what inspired this new series about modern form validation.
[…] In the last article in this series, we built a lightweight script (6kb, 2.7kb minified) using the Validity State API to enhance the native form validation experience.
' 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.
And we wrote a polyfill to extend support […] You can do and impressive amount of form validation with just HTML attributes.
any of the more common questions asked of the VB Pro involve formatting and validating data.
In this 10-Minute Solution, you'll learn how to validate and format a phone number.
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.