JavaScript instance tutorial (9) Test form validity

xiaoxiao2021-03-06  46

Javascript tutorial examples (9) the effectiveness of the inspection form: ZSC Source: Network Hits:

As a result, you can find that if you forget to enter a particular type of data to the form, it will warn you and cancel the submit to the server. Until you type the correct information, it sent the form to the server. Let's take a good study of how the validity test is performed in JavaScript. Where the most important code in the form validity test is a time processor at the form tag, this time processor (onSubmit) must return the form when the true value is returned. The following is a detailed form code:

When Submit (Submit) button is When you press it, this time processor is triggered, which then performs FormCheck (), which is used to check if there is no error in the form. The code of the FormCheck () function is as follows: function formCheck () {if (Document.Theform.user_name.value == ") {Alert (" "please put in a name."); return false;} if (Document.TheForm. Email.Value.indexof ("@") == -1 || Document.Theform.Email.Value == "") {Alert ("" please include a prot email address "); return false;}} First, FormCheck () The function determines if the username input is empty. If it is empty, it will also warn the user and return false values, which will not be sent to the server. The FormCheck () function is then determined whether the email address input contains a @ symbol or empty. If there is no case containing @ symbols or empty, it will warn the user and return false values ​​so that this form will not be sent to the server. Any of the above errors will not send a form to the server until the form is sent to the server after filling out the entire form. The principle of the above procedures is the value "value" of each element in the form, then add the content equal to "==", or the intrinsic content inherent (such as email @). Click here to expand its function, such as perfecting the email's inspection function, plus the address of the mail server after the "@", ".". As shown below: You can also increase the verification of the message content. The added code is the value of the sending content text box: Document.TheForm. Text box name .value == "Click here to test the effect page and get the source code.

转载请注明原文地址:https://www.9cbs.com/read-79323.html

New Post(0)