Chapter 6 JavaScript DOM learning form example

Source: Internet
Author: User

Form instance
This is the strength of a form. Here is a small problem: Because my server does not support it now, forms cannot be submitted. I will print your input and return a false value so that the form is not submitted.
OnSubmit Code does two things: Check whether you have filled in data in the four text boxes, and connect all the elements to print them in the text area below.
There are examples in the original article. If you need kids shoes, please move them here. I will not move them here.
Detect text area
This code checks whether the user has entered the content in the text box. This option ignores the check box and single sequence, but always reminds the user to select the select box. Even if you select it, it will remind him that his value is always null. So it is best to use this code when you just use it to detect text segments.
Copy codeThe Code is as follows:
Function checkscript (){
For (I = 0; I <4; I ++ ){
Box = document. example. elements [I];
If (! Box. value ){
Alert ('You haven \'t filled in '+ box. name + '! ');
Box. focus ()
Return false;
}
}
Return true;
}

In this example, I want to check the elements 0-3, so I set a variable I. You will notice that I use numbers instead of names. This is an example of a number that is better than a name.
Copy codeThe Code is as follows: for (I = 0; I <4; I ++ ){
Then I create a variable box to access the current element. If you do not create a file, write document. example. elements [I] several times.
Copy codeThe Code is as follows: box = document. example. elements [I];
If the value of this text box is empty, we need:
Copy codeThe Code is as follows: if (! Box. value ){
First, we will use the name of the text box. If you have a clear name, you will also better understand which text box contains the problem.
Copy codeThe Code is as follows: alert ('You haven \'t filled in '+ box. name + '! ');
As an additional service, we place the focus on the problematic text box so that the user can enter it immediately. Because all browsers support this function, you do not need to perform the following checks:
Copy codeThe Code is as follows: box. focus ()
Then, a false value is returned. The Code stops running and the form is not submitted. Wait for user input.
Copy codeThe Code is as follows: return false;
If all the text boxes are filled in, true is returned to indicate that everything is normal. Stop the function and submit the form.
Copy codeThe Code is as follows:
}
}
Return true;

Http://www.quirksmode.org/js/formex.html
Reprinted please keep the following information
Author: Beiyu (tw: @ rehawk)

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.