FormValid0.5版本发布,带ajax自定义验证例子
程序员文章站
2022-10-09 10:31:02
最新更新修改了当input元件name有包含[],把焦点移到元件上时会出现js错误,只有ie是这种情况,没找到很好的解决办法,我只是把这种情况给排除了,就是当元件name有...
最新更新修改了当input元件name有包含[],把焦点移到元件上时会出现js错误,只有ie是这种情况,没找到很好的解决办法,我只是把这种情况给排除了,就是当元件name有包含[]不把焦点移上去,哪果有好的解决办法请联系我。
并作了 demo8 - 结合jquery实现ajax自定义验证
下载:
并作了 demo8 - 结合jquery实现ajax自定义验证
下载:
复制代码 代码如下:
/*
* copyright (c) 2006-2008 coderhome.net
* all rights reserved.
* support : 志凡(dzjzmj@163.com)
*
* version : 0.5
*/
var formvalid = function(frm) {
this.frm = frm;
this.errmsg = new array();
this.errname = new array();
this.required = function(inputobj) {
if (typeof(inputobj) == "undefined" || inputobj.value.trim() == "") {
return false;
}
return true;
}
this.eqaul = function(inputobj, formelements) {
var fstobj = inputobj;
var sndobj = formelements[inputobj.getattribute('eqaulname')];
if (fstobj != null && sndobj != null) {
if (fstobj.value != sndobj.value) {
return false;
}
}
return true;
}
this.gt = function(inputobj, formelements) {
var fstobj = inputobj;
var sndobj = formelements[inputobj.getattribute('eqaulname')];
if (fstobj != null && sndobj != null && fstobj.value.trim()!='' && sndobj.value.trim()!='') {
if (fstobj.value <= sndobj.value) {
return false;
}
}
return true;
}
this.compare = function(inputobj, formelements) {
var fstobj = inputobj;
var sndobj = formelements[inputobj.getattribute('objectname')];
if (fstobj != null && sndobj != null && fstobj.value.trim()!='' && sndobj.value.trim()!='') {
if (!eval('fstobj.value' + inputobj.getattribute('operate') + 'sndobj.value')) {
return false;
}
}
return true;
}
this.limit = function (inputobj) {
var len = inputobj.value.length;
if (len) {
var minv = inputobj.getattribute('min');
var maxv = inputobj.getattribute('max');
minv = minv || 0;
maxv = maxv || number.max_value;
return minv <= len && len <= maxv;
}
return true;
}
this.range = function (inputobj) {
var val = parseint(inputobj.value);
if (inputobj.value) {
var minv = inputobj.getattribute('min');
var maxv = inputobj.getattribute('max');
minv = minv || 0;
maxv = maxv || number.max_value;
return minv <= val && val <= maxv;
}
return true;
}
this.requirechecked = function (inputobj) {
var minv = inputobj.getattribute('min');
var maxv = inputobj.getattribute('max');
minv = minv || 1;
maxv = maxv || number.max_value;
var checked = 0;
var groups = document.getelementsbyname(inputobj.name);
for(var i=0;i<groups.length;i++) {
if(groups[i].checked) checked++;
}
return minv <= checked && checked <= maxv;
}
this.filter = function (inputobj) {
var value = inputobj.value;
var allow = inputobj.getattribute('allow');
if (value.trim()) {
return new regexp("^.+\.(?=ext)(ext)$".replace(/ext/g, allow.split(/\s*,\s*/).join("|")), "gi").test(value);
}
return true;
}
this.isno = function (inputobj) {
var value = inputobj.value;
var novalue = inputobj.getattribute('novalue');
return value!=novalue;
}
this.checkreg = function(inputobj, reg, msg) {
inputobj.value = inputobj.value.trim();
if (inputobj.value == '') {
return;
} else {
if (!reg.test(inputobj.value)) {
this.adderrormsg(inputobj.name,msg);
}
}
}
this.passed = function() {
if (this.errmsg.length > 0) {
formvalid.showerror(this.errmsg,this.errname,this.frm.name);
if (this.errname[0].indexof('[')==-1) {
frt = document.getelementsbyname(this.errname[0])[0];
if (frt.type!='radio' && frt.type!='checkbox') {
frt.focus();
}
}
return false;
} else {
return formvalid.succeed();
}
}
this.adderrormsg = function(name,str) {
this.errmsg.push(str);
this.errname.push(name);
}
this.addallname = function(name) {
formvalid.allname.push(name);
}
}
formvalid.allname = new array();
formvalid.showerror = function(errmsg) {
var msg = "";
for (i = 0; i < errmsg.length; i++) {
msg += "- " + errmsg[i] + "\n";
}
alert(msg);
}
formvalid.succeed = function () {
return true;
}
function validator(frm) {
var formelements = frm.elements;
var fv = new formvalid(frm);
for (var i=0; i<formelements.length;i++) {
var validtype = formelements[i].getattribute('valid');
var errormsg = formelements[i].getattribute('errmsg');
if (!errormsg) {
errormsg = '';
}
if (validtype==null) continue;
fv.addallname(formelements[i].name);
var vts = validtype.split('|');
var ems = errormsg.split('|');
for (var j=0; j<vts.length; j++) {
var curvalidtype = vts[j];
var curerrormsg = ems[j];
switch (curvalidtype) {
case 'isnumber':
case 'isemail':
case 'isphone':
case 'ismobile':
case 'isidcard':
case 'ismoney':
case 'iszip':
case 'isqq':
case 'isint':
case 'isenglish':
case 'ischinese':
case 'isurl':
case 'isdate':
case 'istime':
fv.checkreg(formelements[i],regexps[curvalidtype],curerrormsg);
break;
case 'regexp':
fv.checkreg(formelements[i],new regexp(formelements[i].getattribute('regexp'),"g"),curerrormsg);
break;
case 'custom':
if (!eval(formelements[i].getattribute('custom')+'(formelements[i],formelements)')) {
fv.adderrormsg(formelements[i].name,curerrormsg);
}
break;
default :
if (!eval('fv.'+curvalidtype+'(formelements[i],formelements)')) {
fv.adderrormsg(formelements[i].name,curerrormsg);
}
break;
}
}
}
return fv.passed();
}
string.prototype.trim = function() {
return this.replace(/^\s*|\s*$/g, "");
}
var regexps = function(){};
regexps.isnumber = /^[-\+]?\d+(\.\d+)?$/;
regexps.isemail = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-za-z]{2,4}|[0-9]{1,3})(\]?)/;
regexps.isphone = /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
regexps.ismobile = /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/;
regexps.isidcard = /(^\d{15}$)|(^\d{17}[0-9xx]$)/;
regexps.ismoney = /^\d+(\.\d+)?$/;
regexps.iszip = /^[1-9]\d{5}$/;
regexps.isqq = /^[1-9]\d{4,10}$/;
regexps.isint = /^[-\+]?\d+$/;
regexps.isenglish = /^[a-za-z]+$/;
regexps.ischinese = /^[\u0391-\uffe5]+$/;
regexps.isurl = /^http:\/\/[a-za-z0-9]+\.[a-za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
regexps.isdate = /^\d{4}-\d{1,2}-\d{1,2}$/;
regexps.istime = /^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}$/;
上一篇: 用javascript判断输入数据是否货币并自动添加¥符号的代码
下一篇: 芹菜是西芹吗,怎么区分