[Translation] Chapter 6 of Javascript DOM learning: Form instances

Source: Internet
Author: User

In this chapter, I have a form and code that checks user input and prints the user input. Next I will focus on the code for Text Detection.

 

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.

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.

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.

box = document.example.elements[i];

If the value of this text box is empty, we need:

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.

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:

box.focus()

Then, a false value is returned. The Code stops running and the form is not submitted. Wait for user input.

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.

}}return true;

 

Http://www.quirksmode.org/js/formex.html

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.