/**Form object
encapsulates common form operations,
Form.isChinese(str); Verify whether str is Chinese
Form.urlEncode(str); url encoding
Form.getFormElements(formid); Get the form The most commonly used method of element array
Form.getForm(formid); gets the form’s data string
*/
var Form=function (){
//Chinese
this.isChinese=function(str){
return /[u4e00-u9fa5]/.test(str);
}
//Non-alphanumeric Underscore
this.isSpecial=function(str){
return /W/.test(str);
}
//Address encoding
this.urlEncode=function(str){
return encodeURI(encodeURI(str));
}
//Password input level, maximum level 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; ;
return Math.ceil((r1 r2)/2);
}
//Get 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;
}
/*Get form data
1> Non-null verification
2>Chinese encryption, server uses utf-8 to decrypt
form's id, the option format is as follows
var opts={nameIdError: "The form element must have a name or id", valueError: "The value is empty"};
@result: is the request string format, such as? 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.target.focus();
return;
}
alert(result);
*/
this.getForm=function(form,options){
var defNameErr="Form element must have name or id";
var defValueErr="Value is empty";
var params =[];//Parameter array
var err={};//Exception object
var elements=this.getFormElements(form);
for(var i=0;ivar 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("&");
}
};