Java Web basics: 13th client Verification

Source: Internet
Author: User

 

This article is excerpted from page 51-53 of Java Web development tutorial-getting started and improving (JSP + servlet. In Web applications, the input information must be verified on the client. If an error is found, you can give feedback to the user in a timely manner without waiting for the server to discover the error. On the one hand, the user's waiting time is reduced, on the other hand, unnecessary interaction processes are reduced. This section describes how to use JavaScript to verify the client. Javascript LanguageThe functions that need to be executed on the client in Web applications can be written in Javascript language. When using the functions, You need to place the JavaScript code between the following two identifiers: <script language = "JavaScript"> related JavaScript code </SCRIPT> JavaScript code is similar to Java code, but more flexible. This book only introduces JavaScript code related to client verification, if you want to learn more deeply, you can refer to special books. The method format is as follows: Function Method Name (parameter) {// method body} does not need to write the return value type of the method, however, methods can return values and can return values of various types. The parameter type of the method can also be left empty. Place the method between the start ID and end ID of JavaScript. Use form dataTo verify user input information, you must obtain the input information first. Each form element belongs to a form. To obtain information, you must first obtain the form and then access the value of the form element. There are two ways to get form. The first way is to get form: form = Document. form1 through the document object, where form1 is the name of the form element. Another method is to use form as a parameter when calling the verification method. This method is used in the instance in this chapter: isvalidate (form1) Where form1 is the form name. After the form is obtained, the code for getting the value of the form element is as follows: form1.userid. value where userid is the form element and value is the value of the form element. Call Verification MethodVerification is usually performed before the form is submitted. How do I perform verification before the information is submitted? You can use the onclick event of the button or the onsubmit event of the form. The examples in this chapter are completed through the onsubmit event of Form: <form name = "form1" Action = "register_confirm.jsp" method = "Post" onsubmit = "Return isvalidate (form1) "> to use The onclick event of the button, use the following code: <input type =" button "value =" Submit "onclick =" javascript: isvalidate () "> if you use this method, you need to write the code for submitting the form after the verification is passed: document. form1.submit (); Prompt user informationAfter verification fails, a user error message is usually prompted. You can use the following code: Alert ("the address length is greater than 50 characters! "); Use alert to prompt the error message. The parameter is the error message to be displayed. The above prompt is displayed when the length of the address information entered by the user is greater than 50. If the verification fails, the cursor is usually placed in the corresponding input box. You can use the following code: form. address. focus (); if the address length is inappropriate, the cursor is placed in the address input box. Common MethodsSome methods are frequently used during verification, mainly related to string processing. If STR is a string, the following lists the common methods for STR processing: Str. length, indicating the length of the string; Str. charat (I), used to obtain the I-th character in the string, where I starts from 0; Str. indexof (c) is used to find the position of a character. The returned value is the first position that appears. It starts from 0. If it is not found,-1; STR is returned. substring (index1, index2) to obtain the substring. The first parameter indicates the start position, and the second parameter indicates the end position. The substring includes the first parameter, excluding the second parameter. If there is only one parameter, obtain the substring from this parameter to the end of the string. The method becomes Str. substring (INDEX );. STR = another string to determine whether it is the same as the other string. Last Lecture: 12th use of Form ElementsNext Lecture: 14th common client verification methodsLi xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5

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.