I could have titled this post "principles of form validation," because what we will see how it is possible to write a simple form validation script from scratch.
You can use the popular jQuery plugin called just
- Validation
- created by Jörn Zaeferrer , which adds in a very simple custom function to validate a form. We will not use this road here, but look closely at what the problems and needs which must be faced if we want to validate a form with jQuery, and you'll get the flexibility that results from a greater understanding. Our goal is to have a final form that is valid without the need to load another page and send an email formatted arbitrarily, and that while it is flexible enough to support changes to the general recipe. The example comes from the contact form that I have implemented the "sleek" Site www.bfelettrotecnica.it , and the base from which shall be equal to that proposed in this tutorial
- of Net Tuts + ( to the work of Eric
- ), and that the Board of reading, though, before you begin.
1. HTML markup
As you can see from the picture, the form contains many fields, many mandatory (but not all), a select, textarea, a radio and even two for a unique selection: allows the user to choose whether to represent an individual or a company, and fill out the fields that vary According to this choice.
Here is the code of the form:
\u0026lt;div id="contactform">
\u0026lt;form action="#" method="post"> \u0026lt;fieldset> \u0026lt;legend> Request Information \u0026lt; / legend> \u0026lt;ol> \u0026lt;li>
\u0026lt;input id = "private" type = "radio" value = "private" name = "type" /> \u0026lt;span class="radioin"> Private \u0026lt;/ span> /> \u0026lt;label for="nome"> Name * \u0026lt;i> \u0026lt; / i> \u0026lt;/ label> \u0026lt;input id="nome" name="nome" type="text" class="obb" /> \u0026lt;label id='nome_err' class='error'> ; enter your name \u0026lt;/ label> \u0026lt;/ li> \u0026lt;li> \u0026lt;input id = "company" type = "radio" value = "company" name = "type" /> \u0026lt;span class="radioin"> Company \u0026lt;/ span> /> \u0026lt;label for="azienda"> \u0026lt;i> Company Name * \u0026lt;/ i> \u0026lt;/ label> \u0026lt;input
id="azienda" name="azienda" type="text" class="obb" />
\u0026lt;label id='azienda_err' class='error'> Enter the name \u0026lt;/ label> \u0026lt;label for="referente"> Contact \u0026lt;i> * \u0026lt;/ i> \u0026lt;/ label> \u0026lt;input id="referente" name="referente" type="text" class="obb" />
\u0026lt;label id='referente_err' class='error'> enter the name of a contact \u0026lt; / label> \u0026lt;/ li>
\u0026lt;li style="margin-top: 15px;">
\u0026lt;label for="indirizzo"> \u0026lt;i> Address * \u0026lt;/ i> \u0026lt; / label> \u0026lt;input\u0026lt;li> \u0026lt;label for="citta"> City \u0026lt;i> * \u0026lt;/ i> \u0026lt;/ label> \u0026lt;input
id="indirizzo" name="indirizzo" type="text" class="obb"
/> \u0026lt;label id = 'indirizzo_err' class = 'error'> enter the address of \u0026lt;/ label>
\u0026lt;label style="width: 75px;" for="cap"> CAP \u0026lt;i> * \u0026lt; , / i> \u0026lt;/ label> \u0026lt;input
style="width: 50px" id="cap" name="cap" type="text" class="obb"
/> \u0026lt;label id = ' cap_err 'class =' \u200b\u200berror '> enter the zip code \u0026lt;/ label>
\u0026lt;/ li>
id="citta" name="citta" type="text" class="obb" /> \u0026lt;label id = 'citta_err 'class =' \u200b\u200berror '> type the name of the city \u0026lt;/ label>
\u0026lt;label style="width: 75px;" for="provincia"> \u0026lt;i> Province * \u0026lt; / i> \u0026lt;/ label> \u0026lt;input style="width: 50px;" id="provincia" name="provincia" type="text" class="obb" /> \u0026lt;label id='provincia_err' class='error'> enter the province code \u0026lt;/ label>
\u0026lt;/ li> \u0026lt;li> \u0026lt;label for="telefono"> Phone \u0026lt;/ label> \u0026lt;input id="telefono" name="telefono"
type="text" /> \u0026lt;/ li>
\u0026lt;input id = "email" name = "email" type = "text" class = "OBB" /> \u0026lt;label id='email_err' class='error'> enter your e-mail \u0026lt;/ label> \u0026lt;/ li> \u0026lt;label for="area"> Area of \u200b\u200bInterest \u0026lt;/ label>
\u0026lt;li>
\u0026lt;label for="email"> e-mail \u0026lt;i> * \u0026lt;/ i> \u0026lt;/ label>
\u0026lt;select id="area" name="area"> \u0026lt;option value="void"> \u0026lt;/ option> \u0026lt;option value="centraline"> Control Unit \u0026lt;/ option> \u0026lt;option value="plc"> Programming PLC \u0026lt;/ option> \u0026lt;option value="Automazione"> Industrial Automation \u0026lt;/ option> \u0026lt;option
value="Quadri Elettrici"> Switchboards \u0026lt;/ option>
\u0026lt;option value = "Electrical Systems" > Electrical \u0026lt;/ option>
\u0026lt;/ select>
\u0026lt;/ li>
\u0026lt;li> \u0026lt;label for="messaggio"> Message: \u0026lt;i> * \u0026lt;/ i> \u0026lt; ; / label> \u0026lt;textarea id = "post" name = "message" rows = "5" cols = "38" class = "OBB"> \u0026lt;/ textarea> \u0026lt;label id='messaggio_err' class='error'> write the Your request \u0026lt;/ label> \u0026lt;/ li> \u0026lt;/ ol> \u0026lt;/ fieldset> \u0026lt;span id="input"> \u0026lt;input id = "submit" type = "submit" value = "Send Message" /> \u0026lt;/ span> \u0026lt;span style="font-style: italic;">
Personal data will be processed in accordance with privacy laws. \u0026lt;/ span>
\u0026lt;/ form>
the first line, action = "#" , because we will use jQuery to handle all of the part relating to the transfer of data php script that will send the final email.
tags
\u0026lt;label>
from class
error are error messages that appear if the respective required field has not been completed. They are hidden by default.As per specifications of the GET method of php.
Required fields are denoted as usual with an asterisk (*), contained in a tag italics
\u0026lt;i>
, And class
OBB.
Tag
\u0026lt;div>
that has all the form will be required when viewing a success message to the user.
class "error" is dedicated to error messages, while the class "errorinput" is incorrectly filled fields. Need something that catches your eye, and in these cases the color of choice is red. I used something like this, passed through the CSS of the page:
.
error {color: red;
font-style: italic;
float: left;}
. Errorinput
{border: 1px solid red ;
background: # ffcbcb;
color: # 000;
margin-bottom: .5 em;}
property
float
I need the rows with two input tags, not to destroy the layout of the form.
2. jQuery: Form validation
2.0 basic validation
The first thing to do is hide the error messages and take possession of the event click on the submit button on the form, to perform our validation, instead of sending just the facts:
$ (document). ready (function () {$
(. 'error'). hide ();
$ ("# submit"). click (function () {
/ / here we go all the validation that is activated when the user wishes to send the message
}});
So far we are almost verbatim the tutorial
Net Tuts +
.
The validation is now expected to check each input field, and if it is empty, view the error message.
Net Tuts +
has only three fields, and then writes the validation function three times, once for each field: as I have
a lot of fields, I speed things up with a beautiful cycle.
First, you initialize a variable,
datastring
, which will contain all data to pass to the php mail script, and will be in the form:
datastring = [nome_campo_1 =] + [valore_campo_1] + [& nome_campo_2 =] + [valore_campo_2] + ... + [& Nome_campo_n =] + [valore_campo_n]
->
- Then, to analyze each element
- \u0026lt;input> iterative method is used each (callback)
- : \u0026lt;input> for each tag (other than the radio button ' sending one or two of the top radio, and is not a disabled field, as discussed in paragraph 4 below) and for the textarea script must save two pieces of information: the value (with the data entered by the user), obtained by the method val () , and his ID. With this we now create the ID of the corresponding error message by adding the suffix "_err.
- Here is the script for these first steps: $ ("# submit"). Click (function () {$
- (. 'Error'). Hide (); datastring var = "";
{var val = $ (this). val ();
var id = $ (this). attr ("id");
var err = id + "_err"
});}
I quote the statement that hides the error messages here to reset the validation each time you press the submit button.
Now, the validation in itself: if the field is empty (
val == ""
) and the field is required (ie, holding the class
OBB), you must do two actions: Assign error class "errorinput" (seen above) to tag \u0026lt;input>
to color it red,
view the error message, just manipulated by the ID.
If validation passes, it stores the data entered by appending the variable datastring
.
entering these operations, the cycle becomes complete as follows:
{$ (this). AddClass ('errorinput'). Focus ();
$ ("# submit"). Click (function () {var
datastring = "";
$ ('input [id! = Submit] [type! = radio], textarea, select '). not (' input [disabled] '). each (function (i)
{var val = $ (this). val ();
var id = $ (this .) attr ("id");
var err = id + "_err"
/ / error notification to the required fields
if ((val == "") & & ($ (this). is ( '. OBB')))
$ ("label [id =" + err + "]"). slideDown (); return true;} else {
$ ( this). removeClass ('errorinput'), / / \u200b\u200bremoval of protective class error
/ / storing data assessed if (i == 0) { datastring = id + '=' + val;} else { datastring datastring + = '&' + id + '=' + val;}
}});
}});
0 comments:
Post a Comment