c

We shall start to understand this clause{article} with more complex  and is functional - finished scripts. We shall pass on steps through all stages - since production of a problem  and finishing{stopping} the universal script ready to the use. Also we shall start with check of the form before sending on the server.

The general{common} reasons and html-code of the form


Check of the form, perhaps, one of most often used functions. The rare site does without its{her} any variation, whether it be simple sending of the message on e-mail or the form of the complex  order in Internet - shop. The advantage{benefit} of a script is obvious - to check up, that the user has filled in all necessary fields before sending and by that to avoid a problem of reception of empty letters or absence of the contact information of the sender.


Let's assume, that the form at us already is and will consist of the following of 4 fields: a name, the electronic address, a subject of the message and directly @ message. The corresponding html-code for such form will look approximately so:



<form action = "/cgi-bin/formmail.cgi " onsubmit = " return sendform (); ">


Your name: * <input type = "text" name = "name"> <br>

The electronic address: * <input type = "text" name = "email"> <br>

Subject of the message: <input type = "text" name = "subject"> <br>

The message: <textarea name = "message"> </textarea> <br> <br>


<input type = "submit" value = "To send">

<input type = "reset" value = "To clear">


</form>


* - fields necessary for filling


Notice, that as against the usual form directly in tege <form> we trace event onsubmit and at his  approach we cause function of check of the form sendform ().


Why such way of a call of function is chosen? In fact it was possible to apply, for example, event onclick? Idle time - at use of event onclick the button "submit" it is necessary to replace the answer the usual button. And, in case in a browser support javascript is switched - off, we cannot send the form (!). Tracking of event onsubmit is deprived this lack since even at the switched - off support of scripts the form will be sent.


Let's consider, that fields necessary for filling at us only two: a name of the visitor and his  electronic address.


If you closely{attentively} look narrowly at a html-code of our form will notice, that near to these fields I have put an asterisk, and at the end of the form have placed a footnote. It is made, certainly, for convenience and elementary respect for the user.


Function of check of the form before sending


And now we shall pass to the main thing - to a spelling of that function that will directly carry out check of the form. Let's think, what from it{her} it is required to us? Well, first, to check up, that the necessary fields are filled, and second - to send the form. In a case if a little from obligatory fields are empty, we need to generate about it the message to the user and to move the cursor on a corresponding element.


For the beginning we shall write the general{common} obvjazku to function:



<script language = "javascript">

<!--


function sendform () {


// Here we shall place a code of function


return true;

}


//->


</script>


The way of a call of function applied by us through event onsubmit as result of job of function demands return of one of logic values: true or false. And, depending on this value, the form either will be sent, or no.


Now we shall try to write the verifying function adhered to the given concrete form. As you remember, fields necessary for filling at us only two: a name of the visitor and his  electronic address. The most simple is to check up contents of each of obligatory fields on absence of the text:



<script language = "javascript">

<!--


function sendform () {


if (document.forms [0] .name.value == " ") {

alert (' Please, enter your name ');

document.mailform.name.focus ();

return false

}


if (document.forms [0] .email.value == " ") {

alert (' Please, enter the electronic address ');

document.mailform.email.focus ();

return false

}


return true;

}


//->


As you can see, function of check will consist of two identical blocks differing only by a name of the checked field. Let's comment on each line in these blocks:


First we check, that the given field is empty. And if it so,

We deduce{remove} the message on a mistake by means of the built - in function alert (). After the user will close okoshko, we

Let's use a method focus () and we shall move the cursor on an erroneous field. And, at last,

Let's leave from function, having established a tag of success of performance in false.

In a case if the checked field was not empty the corresponding block is simply passed{missed}. At the miss{passing} of all verifying blocks function as result returns true, that testifies to successful check.


Universal function of check


If it is necessary for us to check up only two or three fields still it is possible to reconcile to such method of check "single", but what if them some tens? And in fact such not a rarity - it is especial in complex  questionnaires. Therefore we modify our function that she did not depend on quantity{amount} of checked fields a little.


First of all we shall create a file where we list names of all fields which demand check:


required = new array ("name", "email");


Such approach will allow us to add and modify very easily the list of obligatory fields without direct change of a code of the function.


In addition to the above described file we shall add one more which will contain the text of a mistake for a concrete field:


required_show = new array (" your name ", " the electronic address ");


It will allow us to vary freely the text about mistakes and correctly to use Russian, instead of udovol`stvovat`sja by indigestible phrases such as " name is not entered ".


Having a file of obligatory fields for filling, all check can be carried out in a cycle. The modified function of check will look here is how:



<script language = "javascript">

<!--


required = new array ("name", "email");

required_show = new array (" your name ", " the electronic address ");


function sendform () {


var i, j;


for (j=0; j <required.length; j ++) {

for (i=0; i <document.forms [0] .length; i ++) {

if (document.forms [0] .elements [i] .name == required [j] ** document.forms [0] .elements [i] .value == " ") {

alert (' Please, enter ' + required_show [j]);

document.forms [0] .elements [i] .focus ();

return false;

}

}

}


return true;

}


//->


</script>


In a cycle there is a check of all fields of the form on concurrence with "obligatory". In case concurrence has taken place, check is carried out similarly to how it has been described above, but with one nuance - introduction of a file with error messages has demanded small updating of function alert () so now the text of a mistake directly depends on behalf of a field.


, in general, and all. The given function is quite universal and with the minimal updatings (in effect - contents of two files) can be adapted for any form.