Textbox Validation

C

Craig G

i have the following code for validating a textbox once a page posts, but
the problem i am having is that the TextChanged event won't fire next time
if the user does not change the value again

One way round this is to clear the value but user's generally like to see
the erroneous data

any ideas?

Cheers,
Craig



****************************************************************************
****
autocomplete needs to be turned off in the HTML for the form
<form id="Form1" method="post" runat="server" autocomplete="off">
****************************************************************************
****

Private Sub FirstNameTextBox_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles FirstNameTextBox.TextChanged

If FirstNameTextBox.Text <> "Craig" Then

ASPNET_MsgBox("Incorrect value")

Set_Focus("FirstNameTextBox")

End If

End Sub

Public Sub ASPNET_MsgBox(ByVal Message As String)

System.Web.HttpContext.Current.Response.Write("<SCRIPT
LANGUAGE=""JavaScript"">" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("alert(""" & Message & """)" &
vbCrLf)
System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

End Sub

Sub Set_Focus(ByVal strControlName)

Dim strScript As String

strScript = "<script language=javascript> document.all(""" & strControlName
& """).focus() </script>"

RegisterStartupScript("focus", strScript)

End Sub
 
I

Ian Frawley

"Craig G" wrote in message

Personally and it is just my opinion BUT the TextChanged event for
validation bluuuuuuuuuuurg. Your causing a postback with the TextChanged
event, which is scary if you are validating numerous fields. Either do the
validation on a single postback via a button or generate a script block
dynamically client side to save postbacks and user irritaion.
 
C

Craig G

sorry should have said.

AutoPostBack is True on the textboxes, so it'll only ever fire the one
textchanged event on leaving the field

this is the way our customer wants it to validate. basically to replicate
their existing system.

open to any other suggestions, as im a bit of a novice at all this!!

Cheers,
Craig
 
I

Ian Frawley

in message

You could sack off the autopostback and squirt some JavaScript into the
client using the Page.RegisterStartupScript method. This way you could do
all the focus setting and validation instantly. As for the autopostback on
textchanged, are you creating the textboxes dynamically?

Ian
 
C

Craig G

you've sorta lost me. mainly due to my lack of knowledge! i've always been a
WinForms developer

but basically its a registration process

there is fields i need to check from a db table, after they lose focus. its
basically to stop/inform the user as soon as possible as its quite a long
winded registration process. so rather than let them fill it all in and then
validate on the save click, i want to check after they lose focus

im not building the textboxes dynamically

Cheers,
Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,037
Messages
2,570,371
Members
47,013
Latest member
JewellChes

Latest Threads

Top