Not long ago, when I registered csdn, I found that every time I left a Textbox Control, an error message may appear.
In the past, ASP was used by clicking the "OK" button and then verifying it when submitting the page. In contrast, Asp. net is more friendly and convenient. I tried it myself:
First, define a Textbox Control,
<Asp: textbox id = "studentid" runat = "server" maxlength = "14" columns = "14" tabindex = "1" autopostback = "true" ontextchanged = "studentid_textchanged" tooltip = "student ID = "number on! "/>
As you know,. Net has a verification control:
<Asp: requiredfieldvalidator controltovalidate = "studentid" errormessage = "student id not lost" runat = "server"/>
Through this function, you can verify the textbox of studentid defined above to check whether it is empty. However, this function is often insufficient, so we need to use the ontextchanged and atuopostback attributes of textbox,
When atuopostback is set to "true", events set in ontextchanged are triggered whenever the textbox loses focus and the content in the textbox changes, we can write the verification code to the ontextchanged event so that we can implement similar functions during csdn registration.
The Code is as follows:
'Check whether the student ID is entered correctly
Sub studentid_textchanged (sender as object, e as eventargs)
Dim studentid_text as string
Studentid_text = studentid. Text
If studentid. Text = "" then
Lable1.text = "student ID cannot be blank"
Elseif Len (studentid_text) <7 then
Lable1.text = "student ID cannot be less than 7 digits"
Elseif Len (studentid_text)> 10 then
Lable1.text = "student ID cannot exceed 10 digits"
Else
Lable1.text = ""
End if
End sub
'Submit the form
</SCRIPT>
<Asp: textbox id = "studentid" runat = "server" maxlength = "14" columns = "14" tabindex = "1" autopostback = "true" ontextchanged = "studentid_textchanged" tooltip = "student ID = "number on! "/>
<Asp: Label id = "lable1" font-size = "14px" forecolor = "# ff3300" runat = "server"/>
<Asp: requiredfieldvalidator controltovalidate = "studentid" errormessage = "student id not lost" runat = "server"/>