Validator verification controls use code

Source: Internet
Author: User

The following is the js Code (it is not elegant when binding objects. I hope you can give me some advice !)
Copy codeThe Code is as follows:
Function validator (obj, option) {// verification object
Var self = this;
If (! (Self instanceof validator ))
Return new validator (obj, option );
Self. source = {'mobile': '^ (13 | 14 | 15 | 18) [0-9] {9} $', 'postcode ': '^ \ d {6} $', 'integer': '^ -? \ D * $ ', 'email': '^ \ w + (-\ w +) | (\\. \ w +) * \ @ [A-Za-z0-9] + ((\\. |-) [A-Za-z0-9] + )*\\. [A-Za-z0-9] + $ ', 'url':' ^ http [s]? : \/([\ W-] + \.) + [\ w-] + ([\ w -./? % & =] *)? $ '};
For (var I in self. source ){
If (I = option. type)
Self. type = self. source [I];
}
Self. tag = 2;
Self. input = obj;
Self. options = option;
Self. tip = document. getElementById (self. options. tips );
Self. text = self. tip. innerHTML;
Self. init (obj );
}
Validator. prototype. init = function (o ){
Var self = this;
AddEvent (o, 'focal ', function (){
Self. focus ();
});
AddEvent (o, 'blur', function (){
Self. valid ();
});
}
Validator. prototype. valid = function (){
Var self = this;
Var reg = self. options. reg | self. type;
If (new RegExp (reg). test (self. input. value. replace (/\ s/ig ,''))){
Self. tip. className = 'validator _ oncorrect ';
Self. tip. innerHTML = 'entered correctly ';
Self. tag = 1;
} Else {
Self. tip. className = 'validator _ onerror ';
Self. tip. innerHTML = 'sorry, the content you entered does not comply with the rules! ';
Self. tag = 0;
}
}
Validator. prototype. focus = function (){
This. tip. className = 'validator _ onfocus ';
This. tip. innerHTML = this. text;
}
Function addEvent (el, type, fn) {// bind an event
If (el. attachEvent ){
El ['E' + type + fn] = fn; // copy element references under IE to point this to the el object instead of the window
El [type + fn] = function () {el ['E' + type + fn] (window. event );}
El. attachEvent ('on' + type, el [type + fn]);
} Else
El. addEventListener (type, fn, false );
}
// Page call Method
Var inputs = document. getElementsByTagName ('input'); // The method here is strange, not elegant enough, and no optimization method is found.
Var arr = [];
Arr [0] = validator (inputs [0], {type: 'postcode', tips: 'm1 '});
Arr [1] = validator (inputs [1], {type: 'url', tips: 'm2 '});
Arr [2] = validator (inputs [2], {type: 'email ', tips: 'm3 '});
Arr [3] = validator (inputs [3], {type: 'mobile', tips: 'm4 '});
Arr [4] = validator (inputs [4], {type: 'integer', tips: 'm5', reg: '^ -? \ D * $ '});
Function submitForm () {// submit a form filter
Var l = arr. length;
For (var I in arr ){
If (arr [I]. tag = 1)
L --;
Else if (arr [I]. tag = 2 ){
Arr [I]. valid ();
}
}
If (l! = 0) return false;
}

The following is a page demo. A small icon may be missing, and you do not know how to send executable code.
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta http-equiv = "X-UA-Compatible" content = "IE = EmulateIE7"/>
<Meta name = "copyright" content = ""/>
<Meta name = "keywords" content = ""/>
<Meta name = "description" content = ""/>
<Title> Verification Control </title>
<Style>
Body {padding: 0; margin: 0; font: 12px/1.5 Tahoma, Helvetica, Arial, sans-serif ;}
Body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td {margin: 0; padding: 0 ;}
Button, input, select, textarea {font: 12px/1.5 tahoma, arial, simsun, sans-serif ;}
H1, h2, h3, h4, h5, h6 {font-size: 100%; font-weight: normal ;}
Address, cite, dfn, em, var {font-style: normal ;}
Code, kbd, pre, samp {font-family: courier new, courier, monospace ;}
Small {font-size: 12px ;}
Ul, ol {list-style: none ;}
Sup {vertical-align: text-top ;}
Sub {vertical-align: text-bottom ;}
Legend {color: #000 ;}
Fieldset, img {border: 0 ;}
Button, input, select, textarea {font-size: 100% ;}
Table {border-collapse: collapse; border-spacing: 0 ;}
. Clear {clear: both ;}
Html {overflow:-moz-bars-vertical ;}
A {text-decoration: none ;}
A: hover {text-decoration: underline ;}
. Tabs_panel {margin: 10px 0 0 20px ;}
. Wrap {clear: left ;}
. Left {height: 25px; line-height: 25px; width: 160px ;}
. Center {height: auto; padding: 3px; width: 230px ;}
. Right {height: auto; width: 350px ;}
. Left,. center,. right {float: left; margin: 4px ;}
Input.txt {border: 1px solid # CCCCCC; font-size: 14px; height: 20px; line-height: 20px; width: 188px ;}
. Validator_onshow {background: url (".. /images/validator.gif ") no-repeat scroll 4px 4px transparent; border: 1px solid #3196C4; color: #666666; line-height: 20px; padding-left: 25px ;}
. Validator_onerror {background: url (".. /images/validator.gif ") no-repeat scroll 4px-596px # FFF2E9; border: 1px solid # FF6600; color: #666666; line-height: 20px; padding-left: 25px ;}
. Validator_oncorrect {background: url (".. /images/validator.gif ") no-repeat scroll 4px-pixel PX # FFFFFF; border: 1px solid #3196C4; font-size: 12px; line-height: 20px; padding-left: 25px ;}
. Validator_onfocus {background: url (".. /images/validator.gif ") no-repeat scroll 4px-196px # E2F3FF; border: 1px solid #3196C4; color: #666666; line-height: 20px; padding-left: 25px ;}
</Style>
</Head>
<Body>
<H1> verify the Control <Div id = "example" class = "tabs_panel">
<Form method = "post">
<Div class = "wrap">
<Div class = "left"> zip code: </div>
<Div class = "center"> <input type = "text" name = "validator" class = "txt"/> </div>
<Div class = "right"> <div id = "m1" class = "validator_onshow"> the zip code can only contain six digits, which can be used for mailing or express delivery faster. </Div>
</Div>
<Div class = "wrap">
<Div class = "left"> URL: </div>
<Div class = "center"> <input type = "text" name = "validator" class = "txt"/> </div>
<Div class = "right"> <div id = "m2" class = "validator_onshow"> enter the url address correctly </div>
</Div>
<Div class = "wrap">
<Div class = "left"> Email: </div>
<Div class = "center"> <input type = "text" name = "validator" class = "txt"/> </div>
<Div class = "right"> <div id = "m3" class = "validator_onshow"> enter the correct email format with the @ symbol, which is case-insensitive. </Div>
</Div>
<Div class = "wrap">
<Div class = "left"> mobile phone: </div>
<Div class = "center"> <input type = "text" name = "validator" class = "txt"/> </div>
<Div class = "right"> <div id = "m4" class = "validator_onshow"> the mobile phone number can only contain 11 digits. </Div>
</Div>
<Div class = "wrap">
<Div class = "left"> INTEGER: </div>
<Div class = "center"> <input type = "text" name = "validator" class = "txt"/> </div>
<Div class = "right"> <div id = "m5" class = "validator_onshow"> enter an integer </div>
</Div>
<Div class = "clear"> </div>
<Input type = "submit" value = "save" onclick = "return submitForm ()"/>
</Form>
</Div>
</Body>
<Script type = "text/javascript" src = "style/js/validator. js"> </script>
</Html>

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.