Validating form completion using JavaScript. Departure PHP feedback form. Glow effect in jQuery

1. Plugin for creating online forms “jFormer”

Creation of contact forms: feedback, comments, login form, registration form with checking the correctness of information entered.

2. Step-by-step registration form with using jQuery

Neat shape with step by step filling. Below is a form completion indicator.

3. Step by step form

Filling out the form in several steps and checking that it is filled out correctly.

4. contact form for site

Validation of the correctness of the entered information is carried out on the fly before sending the message using javascript.

5. Animated switching between forms using jQuery

Animated switching from using jQuery between the site login form, the registration form and the password recovery field. On the demo page, click on the yellow link to see the effect.

6. Moving out PHP form feedback

A similar solution can be used to give a visitor the opportunity to quickly contact the site owner from any page. On the demo page, click on the arrow below to open the form.

7. PHP registration form using jQuery and CSS3

Form with verification of correctness of information entered.

8. PHP registration form in Facebook style

A nice registration form implemented using CSS, PHP and jQuery.

9. jQuery contact form “SheepIt”

The ability to add new fields before sending a message has been implemented.

10. Fancy AJAX Contact Form

Nice neat PHP feedback form with verification of correctness of information entered. Technologies: CSS, PHP, jQuery.

11. Authorization/registration system on the site

12. Data submission form

With verification of correct filling.

13. jQuery “Contactable” plugin

To implement an outgoing feedback form for quickly sending a message.

14. jQuery plugin"Formwizard"

Plugin for implementing step-by-step forms on the website.

15. Contact form in the style of an old typewriter

16. Glow effect with jQuery

I'm on a scholarly spree these days
I will win your trust through my service,
You write me a promissory note,
So that I don't have any doubts about the payment.

Mephistopheles, in Goethe's Faust

Forms were briefly introduced in the previous chapter as a way to transmit user-entered information over HTTP. They were developed on the web before the advent of JavaScript, with the expectation that interaction with the server occurs when moving to another page.

But their elements are part of the DOM, like the rest of the page, and the DOM elements that represent form fields support several properties and events that other elements do not. This makes it possible to view and manipulate input fields from JavaScript programs and add functionality to classic forms or use forms and fields as the basis for building an application.

Fields

A web form consists of any number of input fields surrounded by a tag. HTML offers many different fields, from simple on/off checkboxes to drop-down lists and text input fields. This book will not discuss all types of fields in detail, but we will give a short overview of them.

Many types of input fields use the . Its type attribute is used to select the style of the field. Here are some common types:

text a one-line text field password is the same as text, but hides the input checkbox on/off switch radio part of the field with multiple choices file allows the user to select a file on his computer

Form fields do not have to appear inside a . They can be placed anywhere on the page. Information from such fields cannot be passed to the server (this is only possible for the entire form), but when we make fields that JavaScript processes, we usually do not need to pass information from the fields via submit.

(text) (password) (checkbox) (radio) (file)

The JavaScript interface for such elements varies depending on the type. We'll look at each of them a little later.

Multi-line text fields have their own tag. The tag must have a closing tag and use the text inside those tags instead of using the value attribute.

one two Three

And the tag is used to create a field that allows the user to select one of the given options.

Pancakes Casserole Ice cream

When the value of a field changes, the “change” event is fired.

Focus

Unlike most elements HTML document, form fields can receive keyboard focus. When you click or otherwise select them, they become active, i.e. main receivers of keyboard input.

If the document has a text field, then the text you type will appear in it only if the field has input focus. Other fields respond differently to the keyboard. For example, it tries to move to an option that contains text that the user enters, and also responds to arrow keys by moving the option selection up and down.

You can control focus from JavaScript using the focus and blur methods. The first one moves focus to the DOM element from which it is called, and the second one removes focus. The value of document.activeElement corresponds to the current element receiving focus.

document .querySelector("input" ).focus();

console .log(document .activeElement.tagName); // → INPUT document .querySelector("input" ).blur(); console .log(document .activeElement.tagName);

// → BODY Some pages require the user to immediately start working on one of the form fields. Using JavaScript, we can give this field focus when the document is loaded, but HTML also has an autofocus attribute that achieves the same result, but tells the browser what our intentions are. In this case, the browser can override this behavior in appropriate cases

, for example when the user has moved focus somewhere else.

Browsers traditionally allow the user to move focus

Tab key

. We can influence the navigation order through the tabindex attribute. In the example, the document will move focus from the text field to the OK button, instead of going through the help link first.

(help) OK

By default, most HTML element types do not receive focus. But adding a tabindex to an element makes it possible for it to receive focus.

Disabled fields All fields can be disabled with the disabled attribute, which also exists as a property of the DOM object element. I'm fine, I'm passed out

Disabled fields do not accept focus or change, and unlike active fields, they usually appear gray and faded.

When a program is in the process of processing a click on a button or other element, which may require communication with the server and take

The name attribute of a field specifies how the value of this field will be determined when sent to the server. It can also be used as a property name when accessing the form's elements property, which acts as both an array-like object (accessed by number) and a map (accessed by name).

Name: Password: Login var form = document .querySelector("form" );

console .log(form.elements.type); // → password console .log(form.elements.password.type);// → password console .log(form.elements.name.form == form);

// → true A button with a type attribute equal to submit submits the form when clicked. Pressing Enter keys

inside a form field has the same effect.

Submitting a form usually means that the browser goes to the page indicated by the attribute

action forms

, using either a GET or POST request. But before that, the “submit” property is fired. It can be handled in JavaScript, and the handler can prevent the default behavior by calling event preventDefault on the object. Value: Save var form = document .querySelector("form" ); form.addEventListener("submit" , function (event ) ( console .log("Saving value" , ​​form.elements.value.value); event.preventDefault(); ));

Intercepting “submit” events is useful in several cases. We can write code that checks the validity of the entered values ​​and immediately shows an error instead of passing the form data. Or we can disable the default form submission and let the program handle the input itself, for example using XMLHttpRequest to send data to the server without reloading the page. Text fields Fields with tags and types text and password, as well as tags, have

common interface

var textarea = document .querySelector("textarea" ); textarea.addEventListener("keydown" , function (event ) ( // The key code for

F2 happens to be 113 if (event.keyCode == 113 ) ( replaceSelection(textarea, "Khasekhemwy" ); event.preventDefault(); ) ));

function replaceSelection (field, word ) ( var from = field.selectionStart, to = field.selectionEnd; field.value = field.value.slice(0 , from ) + word + field.value.slice(to); // Put the cursor after the word field.selectionStart = field.selectionEnd = from + word.length );

The replaceSelection function replaces the currently selected text with the specified word, and moves the cursor to a position after that word so that you can continue typing.

The “change” event for a text field does not fire every time a single character is entered. It fires after the field loses focus and its value has been changed. To instantly respond to changes in a text field, you need to register an “input” event, which fires every time you enter a character, delete text, or otherwise manipulate the contents of the field. In the following example, we see a text field and a counter showing the current length of the entered text.

length: 0 var text = document .querySelector("input" );

var output = document .querySelector("#length" );

text.addEventListener("input" , function () ( output.textContent = text.value.length; ));

Checkmarks and radio buttons

The checkbox is a simple binary switch. Its value can be retrieved or changed through the checked property, which contains a Boolean value.< buttons.length; i++) buttons[i].addEventListener("change" , setColor);

The document.getElementsByName method returns all elements with the given name attribute. The example iterates through them (via a regular for loop, not a forEach, because the collection returned is not a real array) and registers an event handler for each element. Remember that event objects have a target property that refers to the element that fired the event. This is useful for creating event handlers - our handler can be called different elements, and it must have a way to access the current element that called it.

Select fields

Select fields are similar to radio buttons—they also allow you to select from multiple options. But if radio buttons allow us to control the layout of options, then the type of field is determined by the browser.

Select fields have an option that looks more like a list of checkboxes than radio buttons. If the multiple attribute is present, the tag will allow you to select any number of options, rather than just one.

Pancakes Casserole Ice cream

In most browsers appearance fields will be different from the field with the only option selection, which usually looks like a drop-down menu.

Attribute tag size used to set the number of options that are visible at the same time - this way you can influence the appearance of the drop. For example, by assigning size 3, you will see three lines at once, regardless of whether the multiple option is present.

Each tag has a meaning. It can be defined by the value attribute, but if it is not specified, then the value of the tag is determined by the text inside the .. tag. The element's value property reflects the currently selected option. For a field with the ability to select several options, this property is not particularly needed, because it will contain only one of several selected options.

The field tag can be accessed as an array-like object through the options property. Each option has a selected property indicating whether the option is currently selected. The property can also be changed to make the option selected or unselected.

The following example extracts the selected values ​​from the select field and uses them to create a binary number from the bits. Press Ctrl (or Command on a Mac) to select multiple values ​​at once.

0001 0010 0100 1000 = 0 var select = document .querySelector("select" );< select.options.length; i++) { var option = select.options[i]; if (option.selected) number += Number (option.value); } output.textContent = number; }); var output = document .querySelector("#output" );

select.addEventListener("change" , function () ( var number = 0 ; for (var i = 0 ; i modern browsers they also allow you to read files from JavaScript. The field acts as a guard for files. The script cannot simply grab and open a file from the user's computer, but if the user selects a file in this field, the browser allows the script to begin reading the file.

The file field usually looks like a button labeled something like “Select File,” with information about the selected file next to it.

function () ( if (input.files.length > 0 ) ( var file = input.files; console .log("You chose" , file.name); if (file.type) console .log("It has type " , file.type); ) ));

The element's files property is an array-like object (not a real array) containing a list of selected files. Initially it is empty. The element does not have a simple file property because the user can select multiple files at once with the multiple attribute enabled.

Objects in the files property have the properties name (file name), size (file size in bytes), and type (file type in the sense of media type - text/plain or image/jpeg).

What it doesn't have is a property containing the contents of the file. You have to try hard to get the content. Since reading a file from disk takes a long time, the interface must be asynchronous so that the document does not freeze. You can think of the FileReader constructor as an XMLHttpRequest constructor, but for files.

var input = document .querySelector("input" );

input.addEventListener("change" , function () ( Array .prototype.forEach.call(input.files, function (file ) function () ( console .log("File" , file.name, "starts with" , reader .result.slice(0 , 20 )); reader.readAsText(file));

));

Reading a file occurs by creating a FileReader object, registering a “load” event for it, and calling its readAsText method, passing the file to the volume. When the download is complete, the contents of the file are stored in the result property. The example uses Array.prototype.forEach to iterate through the array, since in a normal loop it would be inconvenient to receive the required file and reader objects from the event handler. The variables would be common to all iterations of the loop. FileReaders also has an “error” event when reading a file fails. The error object will be stored in the error property. If you don't want to bother your head with another inconvenient

function readFile (file ) ( return new Promise (function (succeed, fail) ( var reader = new FileReader(); reader.addEventListener("load" , function () ( succeed(reader.result); )); reader.addEventListener ("error" , function () ( fail(reader.error); )); reader.readAsText(file)); Client-side data storage

Simple HTML pages with the addition of JavaScript can be an excellent basis for mini-applications - small auxiliary programs that automate daily tasks. By attaching event handlers to form fields, you can do everything from converting Fahrenheit to Celsius to generating passwords from the main password and website name.

When such an application needs to persist information between sessions, JavaScript variables cannot be used - their values ​​are thrown away every time the page is closed. You could set up a server, connect it to the Internet, and then the application would store your data there. We'll look at this in Chapter 20. But it adds work and complexity to you. Sometimes it is enough to store data in your browser. But how?

You can store string data so that it will survive page reloads - to do this, you need to put it in a localStorage object. It allows string data to be stored under names (which are also strings), as in this example:

LocalStorage.setItem("username" , "marijn" ); console .log(localStorage.getItem("username" )); // → marijn localStorage.removeItem("username" );

The variable in localStorage is stored until it is overwritten, deleted using removeItem or clearing local storage user.

For sites with different domains– different compartments in this storage. That is, data saved from a website in localStorage can only be read or overwritten by scripts from the same site.

Browsers also limit the amount of data they can store, usually a few megabytes. This limitation, coupled with the fact that clogging up people's hard drives is not profitable, prevents disk space from being eaten up.

The following code implements a simple program for taking notes. It stores notes as an object, associating headings with content. It is encoded in JSON and stored in localStorage. The user can select a note through the field and change its text in . An entry is added by clicking a button.

Notes: new var list = document .querySelector("#list" );

function addToList (name) ( var option = document .createElement("option" ); option.textContent = name; list.appendChild(option); ) // Get the list from local storage var notes = JSON .parse(localStorage.getItem( "notes")) || ("what to buy" : "" ); for (var name in notes) if (notes.hasOwnProperty(name)) addToList(name);

function saveToStorage () ( localStorage.setItem("notes" , JSON .stringify(notes)); ) var current = document .querySelector("#currentnote" ); current.value = notes; list.addEventListener("change" , function () ( current.value = notes; )); current.addEventListener("change" , function () ( notes = current.value; saveToStorage(); )); function addNote () ( var name = prompt("Record name" , "" ); if (!name) return ; if (!notes.hasOwnProperty(name)) ( notes = "" ; addToList(name); saveToStorage() ; ) list.value = name; current.value = notes)

The script initializes the notes variable with the value from localStorage, and if it is not there -

simple object

with one entry “what to buy”. An attempt to read a missing field from localStorage will return null. By passing null to JSON.parse, we will get null back. Therefore, you can use the || operator for the default value.

When the data in note changes (adds new entry or the current one changes), the saveToStorage function is called to update the stored field. If we expected to have thousands of records stored, it would be too expensive, and we would have to come up with more

You can get the value and manipulate these fields from JavaScript. Upon change they trigger the “change” event, upon keyboard input – “input”, and many more different keyboard events. They help us catch the moment when the user interacts with the input field. Properties like value (for text fields and select) or checked (for checkboxes and radio buttons) are used to read and write the contents of the fields.

When submitting a form, the “submit” event occurs. JavaScript Handler can then call preventDefault on this event to stop the data transfer. Form elements do not have to be enclosed in .

When the user has selected a file from the hard drive through the file selection field, the FileReader interface will allow us to access the contents of the file from a JavaScript program.

The localStorage and sessionStorage objects can be used to store information in a way that will survive page reloads. The first one saves the data forever (or until the user specifically deletes it), and the second one – until the browser is closed.

Exercises JavaScript Workbench

Make an interface that allows you to write and execute pieces of JavaScript code.

Make a button next to , which, when pressed, will cause the Function constructor from Chapter 10 to wrap the entered text in a function and call it. Convert the function's return value, or any of its errors, into a string and print it after the text field.

return "hi"; Let's go // Your code.

Autocompletion Complete the text field so that when you type text, a list of options appears below it. You have an array possible options

, and you need to show those that begin with the entered text. When the user clicks on a suggested option, it changes the contents of the field to that option. // Builds an array of global variable names, // like "alert", "document", and "scrollTo" var terms = ;

for (var name in window ) terms.push(name);

// Your code.

Conway's Game of Life

Please note that the rules apply to the entire grid at the same time, and not to each of the cells in turn. That is, the counting of the number of neighbors occurs at one moment before the next step, and changes occurring on neighboring cells do not affect the new state of the cell.

Implement the game using any suitable structures. Use Math.random to create random initial populations. Display the field as a grid of checkmarks with a “go to next step” button. When the user turns checkboxes on or off, these changes must be taken into account when calculating the next generation.

Next generation // Your code.

Very often there is a need to add a form to a site, because a form is the most popular way for a user to communicate with a site, and it is very important that the data entered by the user is correct. And in order for the form to be sent with the correct data, it must be checked before doing so. And that’s what we’ll do in this article: form validation in JavaScript.

Let's first create a form that we will then check:


Your name:



Your password:



What's your gender:




Select number:

1
2
3



Your message:



Agree with our rules:




Upload file:







This is the form. I hope that you are familiar with HTML and I think it costs nothing to explain. If something is not clear, then look at how this code works in the browser.

Now let's talk about how to get values ​​from form fields. After all, before checking them, you need to find out what the user wrote there. General form to access the form field like this:

Document.form_name.field_name.value;

That is, we first access the Document object, then we access its to the Form property(via the form name), then to the field name of this form, and finally to the field value. Let's output all the fields that obey this general view:

Var form = document.form1;
document.write(form.firstname.value + "
");
document.write(form.pass.value + "
");
document.write(form.number.value + "
");
document.write(form.message.value + "
");
document.write(form.rules.value + "
");
document.write(form.hidefield.value + "
");
document.write(form.fileupload.value + "
");
document.write(form.sub.value + "
");
document.write(form.but.value + "
");

These are all fields that have a value property and can be accessed this way.

Now let's talk about one special form element - radio. Let's access the radio value:

Var sex = (document.form1.sex.checked)?
document.form1.sex.value: document.form1.sex.value;

Notice that we have two radio elements that are in the sex array. Their indices are 0 and 1. In this script we check if our first element is included ( checked), then we assign the value of the first element, otherwise we assign the value of the second element. By the way, if anyone doesn’t understand, this is a construction of the IF condition operator. Of course, one could write it like this:

Var sex;
if (document.form1.sex.checked) sex = document.form1.sex.value;
else sex = document.form1.sex.value;

Now that we have access to all the fields, let's finally validate the form. But first, let's add the "onSubmit" attribute to the tag with the value "return check(); ". This attribute will do the following: before submitting the form, call a function that should return either true or false . If it returns true , then the form will be sent to the server, and if false , then the form will not be submitted.

Now let's create a check() function, which should, firstly, check the entire form, inform the user about errors, and also return true (if the form is completely correct) or false (if the form contains errors).

Function check(form) (
var firstname = form.firstname.value;
var pass = form.pass.value;
var message = form.message.value;
var rules = form.rules.value;
var file = form.fileupload.value;
var bad = "";
if (firstname.length< 3)
bad += "Name is too short" + "\n";
if (firstname.length > 32)
bad += "Name is too long" + "\n";
if (pass.length< 3)
bad += "Password is too short" + "\n";
if (pass.length > 32)
bad += "Password is too long" + "\n";
if (message.length< 3)
bad += "Message too short" + "\n";
if (rules != "on")
bad += "You did not agree with the rules" + "\n";
if (file.length == 0)
bad += "You have not selected a file to download" + "\n";
if (bad != "") (
bad = "The form is filled out incorrectly:" + "\n" + bad;
alert(bad);
return false;
}
return true;
}

In this script, we first get all the data that needs to be checked and write it into variables. Then we create a variable bad , into which we write all incorrect data. Then there is a whole set of IFs that check the fields in the form and write any errors to the bad variable. Then, if the bad variable is not empty (that is, there were errors), then we display a window (alert()) with errors. And we return false so that the form is not submitted. If the bad variable is empty, then we simply return true so that the form is sent for processing in “handler.php”.

Like many web developers, I spend a lot of time testing user interface sites I'm working on.

Automated testing is, of course, a good help in this process, but “pain points” in the interface are often impossible to identify in any way other than using the site. Again and again. This process can get boring very quickly. Especially filling out forms.

I ran into this situation myself a couple of months ago when we were adding the British Pound to the supported currencies when checking out in Wufoo.

For verification, it was necessary to fill out many fields with names, addresses, credit card numbers and everything else that is implied in the calculation process. Of course, a significant part of this data should not have been repeated:

In the end, I made a bookmarklet to quickly fill out forms with test data. (Bookmarklet is a JavaScript program designed as a bookmark in a browser, a hybrid of the English words “bookmark” - bookmark and “applet” - small application. – Approx. transl.)

I chose the bookmarklet form because:

  • it is easy to convey to the user;
  • it is easy to update without user intervention;
  • it runs on any platform without modification.

You can easily make a bookmarklet by creating a new "pen" on CodePen and putting something like this in the HTML:

The user just needs to drag the resulting link to the bookmarks area of ​​his browser. Clicking on a bookmark will run the code on the current page.

For this project, I relied on jQuery to make the code as readable and cross-browser compatible as possible. If anyone needs a version on pure JavaScript, write me a comment – ​​I’ll try to figure something out.

To create random data I used the wonderful Faker library. Its JavaScript implementation can be found here.

Bookmarklet Basics

We can put all the code directly into the link. But this option will be inconvenient to read and maintain. Additionally, I wanted to be able to make changes to the code without forcing users to update the bookmark. So it’s better to leave a link to the external file in the bookmarklet.

The bookmarklet itself looks like this:

My bookmarklet

In it we load the script into the body of the page and add a link to the external JavaScript file there. It should be noted that if the bookmarklet is used to test a page loaded via https, then the external JS file must also be served via https, otherwise the browser will throw a security error.

It is quite possible that when developing a bookmarklet you will want to use a local server. In this case, be sure to replace the address in the link with the address of the real server when you distribute your script.

Filling in the fields

Now that we're done with the bookmark that loads our script, we can move on to the script itself.

The most important task for us will be not to break anything in the page that our script will be testing.

So we'll wrap it in a self-starting anonymous function. This will limit the scope of our functions and variables accordingly:

I really like the generator function pseudorandom numbers, cited by Chris Coyler in this article. Let's use it as needed:

var _rand = function(min, max) ( return Math.floor(Math.random() * (max - min + 1)) + min; )

Now let's download FakerJS. Since our script depends on jQuery, we can use its lazy loading mechanism using the $.getScript function:

Now that we've been promised access to Faker, we can use its methods to generate a variety of plausible names, places, addresses, and other test data.

The following constructor will provide us with reuse of the generated data:

var FormData = function(faker) ( this.faker = faker; this.randomWord = faker.Internet.domainWord(); this.username = "fake_" + this.randomWord; this.username += _rand(100.9999); // set this value to your password specifications this.password = "pass1234";

this.name = faker.Name.findName();

// Randomly select dropdown FormData.prototype.randomizeSelect = function(el) ( var $el = $(el), len = $el.find("option").length - 1; $el.children("option") .prop("selected", false) .eq(_rand(1,len + 1)) .prop("selected", true );

// Randomly select radio button FormData.prototype.randomizeRadio = function(radios) ( radios.not(""); var len = radios.length; radios .prop("checked", false) .eq(_rand(0, len - 1)).prop("checked", true);

// Add some lorem text for textareas FormData.prototype.randomizeParagraph = function(el) ( $(el).val(this.faker.Lorem.sentence(5)); );

// Randomize all checkbox fields FormData.prototype.randomizeCheckbox = function(el) ( var $el = $(el); $el.prop("checked", false); if (_rand(0,1) === 0 ) ( $el.prop("checked", true); ) );

FormData.prototype.randomizeEmail = function(el) ( // if we want different emails for all email fields, we can modify this $(el).val("chriscoyier+" + this.randomWord + "@gmail.com"); );

Finally, we need to map the generated data to the various fields of the forms we are testing. We will extract all form fields from our page and fill them with data according to their type and CS S-class. In this form, our script perfectly implements the separation between the mechanism for generating data and the mechanism for using it: var fillForm = function() ( data = new FormData(window.Faker); $("#name").val(data.name); $("#username").val(data.username); $(" #cc").val(data.cc); $("#exp-1").val(data.exp1); $("#exp-2").val(data.exp2); $("# cvv").val(data.cvv); $("#address").val(data.address1); $("#city").val(data.city); $("#state").val (data.state); $("#zip").val(data.zip); $("#pw").val(data.password); $("#pw-repeat").val(data. password); data.randomizeRadio($("")); // Randomize all select boxes $("select").each(function() ( data.randomizeSelect(this); )); // Randomize all checkboxes $( "input) found = true; ) if (!found) ok = false; ) return ok; ) function test(input) ( if (!check(input, "1", "2", "3", "4" , "5", "6", "7", "8", "9", "0", "/", "-", " ")) ( alert("Input not ok."); ) else ( alert("Input ok!"); ) ) // --> Telephone: The test() function determines which of the entered characters are considered correct.
If you want the data entered into the form to be monitored by the server, then you must use the CGI (Common Gateway Interface) interface. The latter allows you to automatically process the data. For example, the server could create a database of information that is accessible to some of the clients. Another example is search pages such as Yahoo. They usually provide a form that allows you to create a query to search in
own database
data. As a result, the user receives a response shortly after clicking on the corresponding button. He does not have to wait until the people responsible for maintaining this server read the data he specified and find the required information. All this is done automatically by the server itself. JavaScript doesn't allow you to do such things. You won't be able to create a book of reader reviews using JavaScript, since JavaScript is unable to write data to any file on the server. You can only do this through the CGI interface. Of course, you can create a review book for which users send information via email.

However, in this case you must enter reviews manually. This can be done if you do not expect to receive 1000 reviews every day.
The corresponding script will be
in plain text
HTML. And no JavaScript programming is needed here at all! Of course, except for the case if you need to check the data entered in the form before sending it - and here you really need JavaScript. I should just add that the mailto command does not work everywhere - for example, there is no support for it in Microsoft Internet Explorer 3.0. Do you like this page?

Not at all.

A waste of time.

The worst site on the web. Parameter enctype="text/plain"

Using the focus() method you can make your form more user-friendly. So, you can choose which element will be highlighted first. Or you can tell the browser to highlight the form where incorrect data was entered. That is, the browser itself will place the cursor on the form element you specify, so that the user does not have to click on the form before entering anything there. You can do this using the following script fragment:

Function setfocus() ( document.first.text1.focus(); )