user input validation

Upload: sheilaabad8766

Post on 26-Feb-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 User Input Validation

    1/16

    User Input ValidationVisual Basic .NET

  • 7/25/2019 User Input Validation

    2/16

    ValidationValidationrefers to ensuring entered data is well, valid.

    Validation helps avoid bugs by filtering out invalid data toget in to the program flow.

  • 7/25/2019 User Input Validation

    3/16

    Basic Validation Process

    Format test- This test determines if the entered string is in thecorrect format.

    Type test- This test checks if the input is the correct type. Forexample: Integers of dates that do not need conversion afterwards.

    Permitted Character test - This test ensure that no illegalcharacters are entered. For example: If a user is supposed to entera name, numbers and some special symbols must not be allowed.

    Range test- This tests checks to see if entered values are within aspecified range. For example : If you are only supposed to type in50 characters, you must not be able to enter more than fifty.

  • 7/25/2019 User Input Validation

    4/16

    Regular Expressions Provide a powerful, flexible, and efficient method

    for processing text.

    Enables you to quickly parse large amounts of textto find specific character patterns;

    To validate text to ensure that it matches a

    predefined pattern (such as an e-mail address);

  • 7/25/2019 User Input Validation

    5/16

    Regular Expressions To extract, edit, replace, or delete text substrings;

    and to add the extracted strings to a collection in

    order to generate a report.

  • 7/25/2019 User Input Validation

    6/16

    RegEx Example - Replacing

    SubString!"#$%&')*'&+",-+.&,/+0123%4.#%+''5$6'7$812+4.3"#2+

    91:25;)1:73565"#3&&+%6 ?')&%560@ A5"63"+'

  • 7/25/2019 User Input Validation

    7/16

    RegEx - Identifying

    Duplicate Words!"#$%&')*'&+",-+.&,/+0123%4.#%+''5$6'7$812+"$87356

    91:25;)1:73565"#3&&+%6 ?')&%560@ AB:

  • 7/25/2019 User Input Validation

    8/16

    RegEx

    The regular expression pattern B:

  • 7/25/2019 User Input Validation

    9/16

    RegEx Methods The Regex.Matchesmethod is called with regular

    expression options set to

    RegexOptions.IgnoreCase. Therefore, the match

    operation is case-insensitive, and the exampleidentifies the substring "This this" as a duplication.

    Note that the input string includes the substring

    "this? This". However, because of the interveningpunctuation mark, it is not identified as a

    duplication.

  • 7/25/2019 User Input Validation

    10/16

    Guided Example Start Visual Studio and create a Desktop VB.NET

    application and design the form to resemble this

    figure.

  • 7/25/2019 User Input Validation

    11/16

    ExampleImportsSystem.Text.RegularExpressions' Regular Expressions Namespace

    PrivateNameValidAsBoolean'Is Name Valid?Private SurnameValid As Boolean 'IsSurnameValid?PrivatePhoneValidAsBoolean'Is Phone Number Valid?Private EmailValid As Boolean 'IsEmailValid?

  • 7/25/2019 User Input Validation

    12/16

    Private Sub txtName_Leave(sender As Object, e As System.EventArgs) HandlestxtName.Leave

    'If Not A Matching Format Entered If Not Regex.Match(txtName.Text, "^[a-z]*$",

    RegexOptions.IgnoreCase).Success Then 'Only Letters

    MessageBox.Show("Please Enter Alphabetic Characters Only!") 'InformUser

    txtName.Focus() 'Return Focus txtName.Clear() 'Clear TextBox

    NameValid = False 'Boolean = False

    Else

    NameValid = True 'Everything Fine

    End If

    End Sub

  • 7/25/2019 User Input Validation

    13/16

    PrivateSubtxtSurname_Leave(sender AsObject,e AsSystem.EventArgs)HandlestxtSurname.Leave

    'Create A Pattern For Surname Dim strSurname As String = "^[a-zA-Z\s]+$"

    Dim reSurname As New Regex(strSurname) 'Attach Pattern To Surname Textbox

    'Not A Match If Not reSurname.IsMatch(txtSurname.Text) Then

    MessageBox.Show("Please Enter Alphabetic Characters Only!")

    txtName.Focus()

    txtName.Clear()

    SurnameValid = False

    Else

    SurnameValid = True

    End If

    End Sub

  • 7/25/2019 User Input Validation

    14/16

    'Function To Check Phone Number Validity Public Function ValidatePhone(ByVal strPhoneNum As String) As Boolean

    ''Create Reg Exp Pattern Dim strPhonePattern As String = "^[1-9]\d{2}-[1-9]\d{2}-\d{4}$"

    'CreateRegExObject DimrePhone AsNewRegex(strPhonePattern)

    'Something Typed In If Not String.IsNullOrEmpty(strPhoneNum) Then

    PhoneValid = rePhone.IsMatch(strPhoneNum) 'CheckValidity

    Else

    PhoneValid=False'Not Valid / Empty

    End If

    Return PhoneValid 'ReturnTrue/False

    EndFunction

    PrivateSubtxtTel_LostFocus(sender AsObject,e AsSystem.EventArgs)HandlestxtTel.LostFocus

    IfNotValidatePhone(txtTel.Text)Then'Call Phone Validation Function

    MessageBox.Show("Please Enter Phone Number In Correct Format!")

    txtTel.Clear() 'ClearInput txtTel.Focus()'Return Focus

    End If

    End Sub

  • 7/25/2019 User Input Validation

    15/16

    PrivateSubValidateEmail()

    'Set Up Reg Exp Pattern To Allow Most Characters, And No Special Characters Dim reEmail As Regex = New Regex("([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\." + _ ")|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", _ RegexOptions.IgnoreCase _ Or RegexOptions.CultureInvariant _ Or RegexOptions.IgnorePatternWhitespace _ Or RegexOptions.Compiled _

    )

    Dim blnPossibleMatch As Boolean = reEmail.IsMatch(txtEmail.Text)

    If blnPossibleMatch Then

    'CheckIfEnteredEmailIsInCorrectFormat IfNottxtEmail.Text.Equals(reEmail.Match(txtEmail.Text).ToString)Then

    MessageBox.Show("Invalid Email Address!")

    Else

    EmailValid=True'Email is Perfect

    End If

    Else 'NotA MatchToPattern

    EmailValid=False'Set Boolean Variable To False

    MessageBox.Show("Invalid Email Address!") 'InformUser

    txtEmail.Clear()'Clear Textbox

    txtEmail.Focus() 'SetFocusToTextBox

    EndIf

    EndSub

    PrivateSubtxtEmail_LostFocus(sender AsObject,e AsSystem.EventArgs)HandlestxtEmail.LostFocus

    ValidateEmail()'Check Email Validity

    End Sub

  • 7/25/2019 User Input Validation

    16/16

    References:

    MSDN

    Code guru