Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating contents of a text box?? 5

Status
Not open for further replies.

ftpdoo

Programmer
Aug 9, 2001
202
GB
Hi,

I have a text box called "txtText" which contains string data. (ie Bla Bla Bla)

I want to validate the contents of this text box to ensure that it either starts with:

"INSERT INTO"
"DELETE FROM"
"UPDATE "

Followed by anything else.. (ie INSERT INTO bla bla bla)

If the txtText contains any other data then display a Message box..

Any idea's??
Thnx,
Jonathan
 
I would use a mid function comparison.

...in the change() or validate() event i would issue a function call...

function isOkayToProceed(strValidate as string) as boolean
dim boolOK as boolean

boolOK = false

if ucase(mid(strValidate, 1, len("insert into"))
= "INSERT INTO" then
boolOK = true
end if

if ucase(mid(strValidate, 1, len("....
end if

isOkayToProceed = boolOK
end function

sub txt_change()
if isOkayToProceed(txt.text) = false then
msgbox "Error..."
end if
end sub
 
NOT CODE, just algoritm,


[tt]
For each TestValue in TestValueList
If (Len(InputValue) <= LenTtestValue) then
isValid = (1 = instr(1, TestValue , InputValue))
else
isValid = (1 = instr(1, InputValue, TestValue ))
endif
next
[/tt]

Should do the job.


Wil Mead
wmead@optonline.net

 
Jonathan,

I would as a quick check just check for the first character having to be an &quot;I&quot; or &quot;D&quot; or &quot;U&quot;...and if that doesn't match then you know it isn't valide...From there I would check based on what that first character was.

I'd do all this using string formating functions like the LEFT function.
Code:
ie. txtText = &quot;Delete From Invoices&quot;

on the change or validate event(prefer validate)

if left(txtText,1) = &quot;I&quot; then
  'check to see if insert is correct here
elseif left(txtText,1) = &quot;D&quot; then
   'delete here
elseif left(txtText,1) = &quot;U&quot; then
   'update here
else 'not valid
   cancel = true 'validate
   txtText = &quot;&quot; or force update before continue in      change event
endif

You may want to UCASE the txtText in the check so that a lowercase won't ruin your validation.

Hope that helps and doesn't confuse to much.

Mavors
Cincinnati, Ohio
 
I'm sorry, I left the algorithm a little broken...

It assumes that you exit the loop on IsValid = true.

Unlike brute force methods, it works for any list of test values and becomes case insensitive rather easily.





Wil Mead
wmead@optonline.net

 
ftpdoo,

This code snipet should do the trick.

Private sub Text1_Validate(cancel as boolean)

if InStr(1, UCase(Text1.Text), &quot;INSERT INTO&quot;) Then
MsgBox &quot;Insert Into&quot;
ElseIf InStr(1, UCase(Text1.Text), &quot;DELETE FROM&quot;) Then
MsgBox &quot;Delete From&quot;
ElseIf InStr(1, UCase(Text1.Text), &quot;UPDATE&quot;) Then
msgbox &quot;Update&quot;
End If

Replace the msgbox with your own code.

Keep'n it simple

reidfl
 
reidfl 's code will chek for proper string, but doesn't check to see if its in the proper place. Change his code (each line) to:

if InStr(1, UCase(Text1.Text), &quot;INSERT INTO&quot;) = 1 Then Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top