JS verification components adopt a policy mode that is easy to expand

Source: Internet
Author: User
Tags hasownproperty

[Javascript]
// JavaScript Document
Var checkObj = {/** data verification class **/
CheckFunc :{}, // set of function objects for data verification
ErrorMessage: [], // error message
RightMessage: [], // correct message
ShowInfoFunc :{}, // function for displaying messages
CheckConfig :{}, // verify the configuration
Check: function (data) {// Verification
Var I, msg, checkType, checker, result_ OK;
For (I in data ){
/* If (data [I]. hasOwnProperty (I )){*/
CheckType = this. checkConfig [I]; // obtain the function name of the corresponding validation rule.
Checker = this. checkFunc [checkType]; // obtain the corresponding verification function
If (! CheckType ){;
Continue; // If the verification rule does not exist, no processing will be performed to jump out of the current traversal and perform the next traversal.
}
If (! Checker ){
Throw new Error ('specify '+ checkType + 'validate function'); // If the verification function does not exist, an exception is thrown.
}
/// HasOwnProperty
Result_ OK = checker. validate (data [I]); // verify a single
If (! Result_ OK ){
// Alert (data [I] + 'error ');
This. errorMessage [I] = I + checker. instructions;
If (this. showInfoFunc [I]) {// if the function is defined to show the status, call this function.
// Alert ('with functions ');
This. showInfoFunc [I] (this. errorMessage [I]);
}
} Else {
// Alert (data [I] + 'true ');
This. rightMessage [I] = I + 'fill in correctly ';
If (this. showInfoFunc [I]) {// if the function is defined to show the status, call this function.
// Alert (this. showInfoFunc [data [I]);
This. showInfoFunc [I] (this. rightMessage [I]);
}
}
} // For in
}, // Check
HasErrors: function () {// check for errors
Var _ count = 0;
For (var I in this. errorMessage ){
_ Count + = 1;
}
Return _ count! = 0;
}
}

/******* Check whether it is empty *******/
CheckObj. checkFunc. isEmpty = {
Validate: function (val ){
Return val! = '';
},
Instructions: 'input value cannot be blank! '// Error message
}
/******* Check whether non-default value is selected ******/
CheckObj. checkFunc. isDefault = {
Validate: function (val ){
Var _ default = 'default ';
Return val! = _ Default;
},
Instructions: 'select a value! '// Error message
}
/***** Check email *****/
CheckObj. checkFunc. isEmail = {
Validate: function (val ){
Var _ reg =/^ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + \. [a-zA-Z] {2, 3} $ /;
Return _ reg. test (val );
},
Instructions: 'enter the correct format! '// Error message
}
/****** Check whether a project is selected **********/
CheckObj. checkFunc. isChecked = {
Validate: function (val ){
Return val;
},
Instructions: 'No value is selected! '// Error message
}

The call method is as follows: www.2cto.com

[Javascript]
Var data = {
FullName: $ ('. content'). find (' # fullnametr'). find ('input'). val (),
Tel: $ ('. content'). find (' # telTr '). find ('input'). val (),
Email: $ ('. content'). find (' # emailTr '). find ('input'). val (),
Address: $ ('. content'). find (' # addressTr '). find ('input'). val (),
Send: $ ('. content '). find ('# sendTr '). find ('span [id = sendWay] '). children ('input: checked '). attr ('rel '),
SendArea: $ ('. content'). find (' # countryTr '). find ('select [name = district]'). val ()
}
***************/
CheckObj. checkConfig = {
FullName: 'isempty ',
Tel: 'isempty ',
Email: 'ismail ',
Address: 'isempty ',
Send: 'ischecked ',
SendArea: 'isdefault'
}
/************ T prompts the function ************/
CheckObj. showInfoFunc = {
FullName: function (msg ){
Trim ('<B class = "red">' + msg + '</B> ');
},
Tel: function (msg ){
Certificate ('.content'{.find('{teltr'}.find('spanceclass?showinfo='{.html ('<B class = "red">' + msg + '</B> ');
},
Email: function (msg ){
Certificate ('.content'{.find('{emailtr'}.find('spanceclass?showinfo='{.html ('<B class = "red">' + msg + '</B> ');
},
Address: function (msg ){
Certificate ('.content'{.find('{ssstr'}.find('span?class=showinfow.'{.html ('<B class = "red">' + msg + '</B> ');
},
Send: function (msg ){
Certificate ('.content'{.find('{sendtr'}.find('span?class=showinfo='{.html ('<B class = "red">' + msg + '</B> ');
},
SendArea: function (msg ){
Certificate ('.content'{.find('{countrytr'}.find('span?class=showinfow.'{.html ('<B class = "red">' + msg + '</B> ');
}
}
/************** Call check ***************/
CheckObj. check (data );
*************/
// Alert (checkObj. rightMessage. lengths );
If (checkObj. hasErrors ()){
Alert ('error occurred ');
}


From the PainsOnline Column

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.