Web engineering requires constant dealing with forms. Every time we need to verify the forms to reduce or block some dirty data, at this time, if we assemble some common form operations into classes, it will be much easier to use them in the future.
The Code is as follows:
/** Form object
Encapsulated common form operations,
Form. isChinese (str); Verify that str is Chinese
Form. urlEncode (str); url Encoding
Form. getFormElements (formid); get the element array of the Form
The most common method
Form. getForm (formid); get the data string of the Form
*/
Var Form = function (){
// Chinese
This. isChinese = function (str ){
Return/[\ u4e00-\ u9fa5]/. test (str );
}
// Non-alphanumeric underscores
This. isSpecial = function (str ){
Return/\ W/. test (str );
}
// Address Encoding
This. urlEncode = function (str ){
Return encodeURI (str ));
}
// Password input level, up to 5
This. checkLevel = function (str ){
Var len = str. length;
Var sLen = str. match (/\ W +/g). join (''). length;
Var r1 = len <8? 1: len> 8 & len <14? 2: len> 14 & len <21? 3: len> 21 & len <28? 4: 5;
Var r2 = sLen> 1 & sLen <2? 1: sLen> 2 & sLen <4? 2: sLen> 4 & sLen <6? 3: sLen> 6 & sLen <8? 4: 5;
Return Math. ceil (r1 + r2)/2 );
}
// Obtain the internal elements of the form
This. getFormElements = function (form ){
Var elements = [];
Var params = form. elements;
For (var I = 0; I Var param = params [I];
Var type = param. type;
If (type! = "" & Type! = "Button" & type! = "Reset" & type! = "Submit") {// non-button, non-image domain
Elements. push (param );
}
}
Return elements;
}
/* Obtain form data
1> non-empty Verification
2> Chinese encryption. The server uses UTF-8 for decryption.
Form's id. The option format is as follows:
Var opts = {nameIdError: "The form element must have name or id", valueError: "The value is blank "};
@ Result: the format of the request string, such? Query = abc
Usage:
Var opts = {nameIdError: "name id error", valueError: "value error "};
Try {
Var result = Form. getForm (form, opts );
} Catch (e ){
Alert (e. message );
E.tar get. focus ();
Return;
}
Alert (result );
*/
This. getForm = function (form, options ){
Var defNameErr = "the form element must have a name or id ";
Var defValueErr = "the value is null ";
Var params = []; // parameter Array
Var err ={}; // exception object
Var elements = this. getFormElements (form );
For (var I = 0; I Var element = elements [I];
Var value = element. value;
Var name = element. name? Element. name: element. id;
If (! Name ){
Err ["target"] = element;
DefNameErr + = "[" + element + "]";
NameIdError + = "[" + element + "]";
Err ["message"] =! Options? DefNameErr: options. nameIdError? Options. nameIdError: defNameErr;
} Else if (! Value ){
Err ["target"] = element;
DefValueErr + = "[" + element + "]";
Options. valueError + = "[" + element + "]";
Err ["message"] =! Options? DefValueErr: options. valueError? Options. valueError: defValueErr;
}
If (err ["target"]) {
Throw err;
}
If (this. isChinese (value )){
Value = this. urlEncode (value );
}
Params. push (name + "=" + value );
}
Return params. join ("&");
}
};