Implementation Code of Form Verification made by jquery plug-in

Source: Internet
Author: User

First go to the page code:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Script type = "text/javascript" src = "Scripts/jquery-1.8.0.min.js"> </script>
<Script type = "text/javascript" src = "Scripts/jquery. formCheck. js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ('. Formtocheck'). formCheck ();
});
</Script>
<Style type = "text/css">
Form
{
Width: 300px;
}
Label
{
Position: absolute;
}
Input: not (. submit), textarea
{
Margin-left: 100px;
Width: 200px;
}
. Submit
{
Margin-left: 100px;
}
. Error
{
Border: 1px solid red;
}
</Style>
</Head>
<Body>
<Form class = "formToCheck" id = "formToCheck" method = "get" action = "#">
<Fieldset>
<Legend> a simple form </legend>
<P>
<Label for = "fname">
* First name </label>
<Input id = "fname" class = "required" name = "fname"/>
</P>
<P>
<Label for = "lname">
Last name </label>
<Input id = "lname" name = "lname"/>
</P>
<P>
<Label for = "email">
* Email </label>
<Input id = "email" name = "email" class = "required email"/>
</P>
<P>
<Label for = "comment">
* Comment </label>
<Input id = "comment" name = "comment" class = "required"/>
</P>
<P>
<Label for = "email">
* Email </label>
<Input id = "submit" type = "submit" class = "submit" value = "submit"/>
</P>
</Fieldset>
</Form>
</Body>
</Html>

We have defined a form and several input boxes. Note the class attribute. Some of them are class = "required", which indicates that this item is required; class = "required email" indicates that this item is required and must comply with the email format. The input box without class definition is not verified. The plug-in code is as follows:
Copy codeThe Code is as follows:
(Function ($ ){
$. Fn. formCheck = function (options ){
Var defaults = {
ErrorClass: 'error'
};
Var options = $. extend (defaults, options );
Return this. each (function (){
Var form = $ (this );
// If it is not a from form, direct return is not performed.
If (! Form. is ('form ')){
Return;
}
// Verification is performed only when the form is submitted.
Form. submit (function (){
Var errorFlag = false;
// Obtain all input controls in the form and process them one by one
$ (': Input', this). each (function (index, item ){
// Obtain the current object
Var element = $ (item );
// Remove a style
Element. removeClass (options. errorClass );
// Required verification. The value cannot be blank.
If (element. hasClass ('required') & element. val (). length = 0 ){
ErrorFlag = true;
Element. addClass (options. errorClass );
}
// Digit Verification
If (element. hasClass ('number') & element. val (). length> 0 &&! /^ \ D + $/. test (element. val ())){
ErrorFlag = true;
Element. addClass (options. errorClass );
}
// Email Verification
If (element. hasClass ('email ') & element. val (). length> 0
&&! /^ [A-zA-Z0-9. _-] + @ [a-zA-Z0-9. -] + \. [a-zA-Z] {2, 4} $ /. test (element. val ())){
ErrorFlag = true;
Element. addClass (options. errorClass );
}
// Verify the numeric Length
Var num = this. className. match (/min (\ d +)/I );
If (num & element. val (). length <num [1]) {
ErrorFlag = true;
Element. addClass (options. errorClass );
}
});
Return! ErrorFlag;
});
});
};
}) (JQuery );

Demo: jQuery. plugin. formcheck
This is the end of today's course.

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.