var fk;
var mode;
validator={
errinput : 'errinput',
errmsg : 'errmsg',
errcls : 'no',
yescls : 'yes',
require : /[^(^\s*)|(\s*$)]/,
email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,
mobile : /^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}?$|15[0-9]\d{8}?$/,
url : /^http:\/\/[a-za-z0-9]+\.[a-za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
idcard : "this.isidcard(value)",
currency : /^\d+(\.\d+)?$/,
number : /^\d+$/,
zip : /^[1-9]\d{5}$/,
ip : /^[\d\.]{7,15}$/,
qq : /^[1-9]\d{4,8}$/,
integer : /^[-\+]?\d+$/,
double : /^[-\+]?\d+(\.\d+)?$/,
english : /^[a-za-z]+$/,
chinese : /^[\u0391-\uffe5]+$/,
username : /^[a-z_ ]\w{3,}$/i,
//unsafe : /^(([a-z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
unsafe : /[<>\?\#\$\*\&;\\\/\[\]\{\}=\(\)\.\^%,]/,
//safestr : /[^#\'\"~\.\*\$&;\\\/\|]/,
issafe : function(str){return !this.unsafe.test(str);},
safestring : "this.issafe(value)",
filter : "this.dofilter(value)",
limit : "this.checklimit(common.strlen(value))",
limitb : "this.checklimit(this.lenb(value))",
date : "this.isdate(value)",
repeat : "this.checkrepeat(value)",
range : "this.checkrange(value)",
compare : "this.checkcompare(value)",
custom : "this.exec(value)",
group : "this.mustchecked()",
ajax: "this.doajax(errindex)",
isidcard : function(number){
var date, ai;
var verify = "10x98765432";
var wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];
var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
if(re == null) return false;
if(re[1] >= area.length || area[re[1]] == "") return false;
if(re[2].length == 12){
ai = number.substr(0, 17);
date = [re[9], re[10], re[11]].join("-");
} else {
ai = number.substr(0, 6) + "19" + number.substr(6);
date = ["19" + re[4], re[5], re[6]].join("-");
}
if(!this.isdate(date, "ymd")) return false;
var sum = 0;
for(var i = 0;i<=16;i++){
sum += ai.charat(i) * wi[i];
}
ai += verify.charat(sum%11);
return (number.length ==15 || number.length == 18 && number == ai);
},
isdate : function(op){
var formatstring = this['element'].attr('format');
formatstring = formatstring || "ymd";
var m, year, month, day;
switch(formatstring){
case "ymd" :
m = op.match(new regexp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
if(m == null ) return false;
day = m[6];
month = m[5]*1;
year = (m[2].length == 4) ? m[2] : getfullyear(parseint(m[3], 10));
break;
case "dmy" :
m = op.match(new regexp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
if(m == null ) return false;
day = m[1];
month = m[3]*1;
year = (m[5].length == 4) ? m[5] : getfullyear(parseint(m[6], 10));
break;
default :
break;
}
if(!parseint(month)) return false;
month = month==0 ?12:month;
var date = new date(year, month-1, day);
return (typeof(date) == "object" && year == date.getfullyear() && month == (date.getmonth()+1) && day == date.getdate());
function getfullyear(y){
return ((y<30 ? "20" : "19") + y)|0;
}
}, //end isdate
dofilter : function(value){
var filter =this['element'].attr('accept');
return new regexp("^.+\.(?=ext)(ext)$".replace(/ext/g,filter.split(/\s*,\s*/).join("|")),"gi").test(value);
},
checklimit:function(len){
var minval=this['element'].attr('min') ||number.min_value;
var maxval=this['element'].attr('max') ||number.max_value;
return (minval<= len && len<=maxval);
},
lenb : function(str){
return str.replace(/[^\x00-\xff]/g,"**").length;
},
checkrepeat:function(value){
var to = this['element'].attr('to');
return value==jquery('input[name="'+to+'"]').eq(0).val();
},
checkrange : function(value){
value = value|0;
var minval=this['element'].attr('min') || number.min_value;
var maxval=this['element'].attr('max') || number.max_value;
return (minval<=value && value<=maxval);
},
checkcompare : function(value){
var compare=this['element'].attr('compare');
if(isnan(value)) return false;
value = parseint(value);
return eval(value+compare);
},
exec : function(value){
var reg = this['element'].attr('regexp');
return new regexp(reg,"gi").test(value);
},
mustchecked : function(){
var tagname=this['element'].attr('name');
var f=this['element'].parents('form');
var n=f.find('input[name="'+tagname+'"][checked]').length;
var count = f.find('input[name="'+tagname+'"]').length;
var minval=this['element'].attr('min') || 1;
var maxval=this['element'].attr('max') || count;
return (minval<=n && n<=maxval);
},
doajax : function(value) {
var fk;
var element = this['element'];
var errindex = this['errindex'];
var url=this['element'].attr('url');
var mode = element.attr('mode') || 1 ;
var msgid = element.attr('msgid');
var val = this['element'].val();
var str_errmsg=this['element'].attr('msg');
var arr_errmsg = str_errmsg.indexof('|') ? str_errmsg.split('|') :str_errmsg;
var errmsg = arr_errmsg[errindex];
var type=this['element'].attr('type');
var errcls=this['errcls'];
var yescls=this['yescls'];
var param = val ?this['element'].attr('param') + '&value=' + val : this['element'].attr('param');
var charset = browser.isie ? document.charset : document.characterset;
var methodtype = (charset.tolowercase() == 'utf-8') ? 'post' : 'get';
var method=this['element'].attr('method') || methodtype;
var s = $.ajax({
type: method,
url: url,
data: param,
cache: false,
async: false,
success: function(data){
data = data.replace(/(^\s*)|(\s*$)/g, "");
if(data != 'success')
{
errmsg = errmsg ? errmsg : data;
fk = false;
(type!='checkbox' && type!='radio' && element.addclass('errinput'));
if(mode == 1)
{
if(msgid)
{
id = '#' + msgid;
$(id).removeclass('yes');
$(id).addclass('no');
$(id).html(errmsg);
}
else
{
jquery("").html(errmsg).insertafter(element);
}
}
else if(mode == 2)
{
alert(errmsg);
}
return false;
}
else
{
fk = true;
if(msgid)
{
id = '#' + msgid;
$(id).removeclass('no');
$(id).addclass('yes');
$(id).html('');
}
else
{
jquery(" ").insertafter(element);
}
return true;
}
}
}).responsetext;
s = s.replace(/(^\s*)|(\s*$)/g, "");
return s == 'success' ? true : false;
}
};
validator.showerr=function (element, errindex){
var str_errmsg=element.attr('msg') ||'unkonwn';
var arr_errmsg = str_errmsg.split('|');
var errmsg = arr_errmsg[errindex] ? arr_errmsg[errindex]: arr_errmsg[0];
var mode = element.attr('mode') || 1;
var msgid= element.attr('msgid');
var type=element.attr('type');
(type!='checkbox' && type!='radio' && element.addclass(this['errinput']));
if(mode == 1)
{
if(msgid)
{
id = '#' + msgid;
$(id).removeclass('yes');
$(id).addclass('no');
$(id).html(errmsg);
}
else
{
jquery("").html(errmsg).insertafter(element);
}
}
else
{
alert(errmsg);
}
}
validator.removeerr = function(element){
element.removeclass(this['errinput']);
element.parent('*').find('span[tag="err"]').remove();
}
validator.checkajax = function(element, datatype, errindex)
{
var value=jquery.trim(element.val());
this['element'] = element;
this['errindex'] = errindex;
validator.removeerr(element);
return eval(this[datatype]);
}
validator.checkdatatype = function(element,datatype){
var value=jquery.trim(element.val());
this['element'] = element;
validator.removeerr(element);
switch(datatype){
case "idcard" :
case "date" :
case "repeat" :
case "range" :
case "compare" :
case "custom" :
case "group" :
case "limit" :
case "limitb" :
case "safestring" :
case "filter" :
return eval(this[datatype]);
break;
default:
return this[datatype].test(value);
break;
}
}
validator.check=function(obj){
var datatype = obj.attr('datatype');
if(typeof(datatype) == "undefined") return true;
if(obj.attr('require')!="true" && obj.val()=="") return true;
var datatypes = datatype.split('|');
var ok = true;
jquery.each(datatypes,function(index,data){
if(typeof(validator[data]) == "undefined") {
ok = false;
return false;
}
if(data != 'ajax')
{
if(validator.checkdatatype(obj,data)==false){
validator.showerr(obj, index);
return ok=false;
}
else
{
var msgid= obj.attr('msgid');
if(msgid)
{
id = '#' + msgid;
$(id).removeclass('no');
$(id).addclass('yes');
$(id).html('');
}
else
{
jquery("").insertafter(obj);
}
}
}
else
{
ok = validator.checkajax(obj, data, index);
}
});
return ok;
}
jquery.fn.checkform = function(m){
mode = (m==1) ? 1 : 0;
var form=jquery(this);
var elements = form.find('input[require],select[require],textarea[require]');
elements.blur(function(index){
return validator.check(jquery(this));
});
form.submit(function(){
var ok = true;
var errindex= new array();
var n=0;
elements.each(function(i){
if(validator.check(jquery(this))==false){
ok = false;
errindex[n++]=i;
};
});
if(ok==false){
elements.eq(errindex[0]).focus().select();
return false;
}
if(document.getelementbyid('video_uploader') && !uploading)
{
uploadfile();
return false;
}
if($('#f_filed_1') && set_show==false)
{
$("select[@id=catids] option").each(function()
{
$(this).attr('selected','selected');
});
}
if($('#hava_checked').val()==0)
{
yp_checkform();
return false;
}
return true;
});
}