JavaScript Dom Learning Sixth Chapter form Instances _ Basics

Source: Internet
Author: User
Form Instances
This is a form of strength. Here is a small problem: because my server is not supported now, the form cannot be submitted. I will print out your input and return a false so that the form is not submitted.
OnSubmit's code does two things: check that you've filled in four text boxes, and then connect all the elements to print in the following text area.
There are examples in the original, need children's shoes please, I will not move over.
Detect text Area
This code detects whether the user has entered content in a text box. He ignores check boxes and radio boxes, but always alerts the user to select box, even if you choose, because his value is always null. So it's best to use this code when you're just testing a text segment.
Copy Code code 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 case, I want to check the 0--3 number element, so I set a variable i. You will notice that I am using numbers instead of using name. This is an example of how numbers are better than name.
Copy Code code as follows:
for (i=0;i<4;i++) {

Then I create a variable box to access the current element. If I don't create it, I'll have to write it several times, and I'm document.example.elements[i to write it.
Copy Code code as follows:
box = Document.example.elements[i];

If the value of this text box is empty, then we need to do the following:
Copy Code code as follows:
if (!box.value) {

First we use the name of the text box, if your name is more clear, then the user will also better understand which text box in addition to the problem.
Copy Code code as follows:
Alert (' You haven\ ' t filled in ' + Box.name + '! ');

As an add-on service, we put the focus on the problematic text box so that the user can fill it out immediately. Because all browsers are supported, there is no need for detection:
Copy Code code as follows:
Box.focus ()

Then we return a false. The code stops running and the form is not committed. Waits for user input.
Copy Code code as follows:
return false;

If all the text boxes are filled in, return true to indicate that everything is OK. function stopped, form submission.
Copy Code code as follows:

}
}
return true;

Translation Address: http://www.quirksmode.org/js/formex.html
Reprint please keep the following information
Author: North Jade (TW: @rehawk)
Related Article

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.