Vb net form validating

Rated 4.58/5 based on 502 customer reviews

Text, " "c) = True Then result = result & "- First Name field has spaces" & vb New Line End If If In Str(txt First Name. Text)) = True Then result = result & "- First Name field has numbers" & vb New Line End If 'last name field validation If txt Last Name. Length = 0 Then result = result & "- Last Name field is empty" & vb New Line End If If In Str(txt Last Name.

Text, " "c) = True Then result = result & "- Last Name field has spaces" & vb New Line End If If In Str(txt Last Name. Text)) = True Then result = result & "- Last Name field has numbers" & vb New Line End If 'major validation If txt Major. Length = 0 Then result = result & "- Major field is empty" & vb New Line End If If In Str(txt Major. Click Dim error Str As String = "" error Str = Validate Fields() If error Str = "" Then 'place all the student field data into the array of elements ary Data(0) = txt First Name.

Is Null Or Empty(str Phone Num) Then Phone Valid = re Phone. Text) Then 'Call Phone Validation Function Message Box. I set up my Expression to allow only numbers, but in the format I explained earlier.

Is Match(str Phone Num) 'Check Validity Else Phone Valid = False 'Not Valid / Empty End If Return Phone Valid 'Return True / False End Function Private Sub txt Tel_Lost Focus(sender As Object, e As System. If it isn't valid input, it will clear the textbox and give it the focus again; if it is valid, the Phone Valid variable gets updated to true.

So, what I am trying to say is: make sure you understand other cultures, or be aware of people with uncommon names. Using Regular expressions here, is basically the standard, even though it is a bit messy. Focus() 'Return Focus End If End Sub Here, I created a separate function to do all the work.

Add this to validate the surname textbox: Private Sub txt Surname_Leave(sender As Object, e As System. Leave 'Create A Pattern For Surname Dim str Surname As String = "^[a-z A-Z\s] $" Dim re Surname As New Regex(str Surname) 'Attach Pattern To Surname Textbox 'Not A Match If Not re Surname. Add the following code to validate your phone number textbox: 'Function To Check Phone Number Validity Public Function Validate Phone(By Val str Phone Num As String) As Boolean ''Create Reg Exp Pattern Dim str Phone Pattern As String = "^[1-9]\d-[1-9]\d-\d$" 'Create Reg Ex Object Dim re Phone As New Regex(str Phone Pattern) 'Something Typed In If Not String. This function is later called inside the textbox's Lost Focus event, which also fires when a control loses the focus. Keep in mind that each country has its own domain name, which sometimes will look like: Private Sub Validate Email() 'Set Up Reg Exp Pattern To Allow Most Characters, And No Special Characters Dim re Email As Regex = New Regex("([a-z A-Z0-9_\-\.] )@((\

First/Last Name must have no spaces, Course must consist of a three letters and two numbers in that order and nothing else(I want to avoid the use of masked text box), and Email must have the "@" sign and a dot. add If statements for the fields you want to validate Private Function Is Quick Admission Valid() As String 'return string value containing the missing pieces if it is invalid. The major field validation with Substring() is messed up.

Text, " "c) = True Then result = result & "- Major field has spaces" & vb New Line End If If Is Numeric(txt Major. Substring(0, 1)) = True Then result = result & "- Major field course code is incorrect" & vb New Line End If 'phone validation If txt Phone. Length = 0 Then result = result & "- Phone field is empty" & vb New Line End If If Is Numeric(txt Phone. Cancel End Sub Private Sub btn Add Student_Click(By Val sender As System.

Text) = False Then result = result & "- Phone field contains illegal characters" & vb New Line End If 'email validation If txt Email. Length = 0 Then result = result & "- Email field is empty" & vb New Line End If If In Str(txt Email. Text, ".") Then result = result & "- Email field is invalid" & vb New Line End If ' GPA validation If txt GPA. Length = 0 Then result = result & "- GPA field is empty" & vb New Line End If If Is Numeric(txt GPA.

Well, sometimes a good structured and thought out If statement can suffice, but it can become quite long and cumbersome in the future. By using Regular Expressions this way, you can reduce the chances of getting errors at erratic times. This sub checks the email's format and if it thinks that it matches, it returns True and vice versa.

Also, the more tests you are performing, the slower your app might become. For more information regarding Regular Expressions, you are welcome to look at these two MSDN articles: Because it is always easy to learn by doing things practically, you will be creating an app that makes use of Regular Expressions to determine valid input. NET application and design the form to resemble Figure 1. Obviously this only works for the formatting of the email.

Leave a Reply