Title: Data Entry Validation Techniques
1Data Entry Validation Techniques Operations on
Strings
2Data EntryValidation on the keyboard level
- InStr function
- ASCII coding scheme
- Chr, Asc functions
- KeyPress event
- Mid statement and function
3InStr function
- to decide if one string is in another string
- InStr(string1, string2)
- string1 is the string being searched, string2 is
the - string being sought
- if string2 is not in string1, InStr returns 0
- if string2 is in string1, InStr returns the
position at - which the match is found
4InStr Example
InStr (abcvt, v) value 4 is returned
InStr(abcvt, s) value 0 is returned
5ASCII Code
- American Standard Code for Information
Interchange
ASCII Character ASCII Character 8
back space 64 _at_ 48
0 65 A 49
1 66 B 50
2 67 C 61
97 a 62
gt 98 b .
6Asc function
- Returns the character code corresponding to the
first - letter in a string
MyNumber Asc("A") ' Returns 65. MyNumber
Asc("a") ' Returns 97. MyNumber Asc("Apple") '
Returns 65.
7Chr function
- Chr function returns the character associated
with the - specified character code
MyChar Chr(65) ' Returns A. MyChar Chr(97) '
Returns a. MyChar Chr(62) ' Returns gt. MyChar
Chr(37) ' Returns .
8(No Transcript)
9KeyPress event
Private Sub Text1_KeyPress(ByVal Sender As
System.Object, ByVal e As KeyPressEventArgs)
Handles Text1.KeyPress Dim validstring
As String validstring "0123456789"
'to check if the key pressed is a member of
validstring If InStr(validstring,
e.KeyChar.ToString) 0 Then
e.Handled True 'input ignored End
If End Sub
Character of the key pressed
10Private Sub Text2_KeyPress(ByVal Sender As
System.Object, ByVal e As KeyPressEventArgs)
Handles Text2.KeyPress Dim validstring
As String validstring "0123456789.-"
'to check if the key pressed is a member of
validstring If InStr(validstring,
e.KeyChar.ToString) 0 Then
e.Handled True 'input ignored End
If End Sub
11 Private Sub Text3_KeyPress(ByVal Sender As
System.Object, ByVal e As KeyPressEventArgs)
Handles Text3.KeyPress Dim i As Short
i Asc(e.KeyChar) Dim validstring
As String If i ltgt 8 Then 'enable
backspace key validstring
"0123456789.-" 'to check if the key
pressed is a member of validstring If
InStr(validstring, e.KeyChar.ToString) 0 Then
e.Handled True 'input
ignored End If End If End
Sub
12Private Sub Text5_KeyPress(ByVal Sender As
System.Object, ByVal e As KeyPressEventArgs)
Handles Text5.KeyPress 'change the
selected text into uppercase
Text5.SelectedText e.KeyChar.ToString.ToUpper
e.Handled True End Sub
13Data Entry Validation Using the Leave Event
Private Sub TextBox1_Leave(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
TextBox1.Leave If Not IsDate(TextBox1.Tex
t) Then MessageBox.Show("Please enter
a date") TextBox1.Text ""
TextBox1.Focus() End If End Sub
14Replacing Characters in Strings
Dim phone as string 614-292-2345 phone
phone.replace(-, ) 6142922345
Dim aWord as string latter aWord
aWord.replace(t, d) ladder
Replacing all occurrences
15Mid Statement
Replace a specified number of characters in a
string.
Dim myString as string myString The dog
jumps Mid(myString, 5, 3) fox myString
The fox jumps Mid(myString, 5) cat
mySring The cat jumps
Index is one based.
16Mid Function
Returns a string containing a specified number of
characters from a string.
Dim myString, firstWord as string myString
Mid function demo firstWord mid(myString, 1,
3) returns Mid firstWord mid(myString,
5) returns function demo
Index is one based.
17Inserting Characters within a String
Dim phone as string 1234567890 Phone
phone.insert(0, () Phone phone.insert(4,
)) Phone phone.insert(8, -) Result
(123)456-7890
Index is zero based.
18How to get the number into the display?