The ontextchange attribute of textbox.

Source: Internet
Author: User
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"/>
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.