好吧写一个自己的表单验证,临时的。
后面还更新:现在依赖jq
//表单验证 function testInput(o,attr,yfn,nfn,run){ var re = { 'age' :/^[^0]d{0,2}$/, 'null' :/s/, 'qq' : /^[1-9][0-9]{4,9}$/, //QQ 'email' : /^w+@[a-z0-9]+(.[a-z]+){1,3}$/, //电子邮箱 'number': /^d+$/, //数字 'mail' :/^[1-9]d{5}$/, //邮政编码 'id' :/^[1-9]d{14}|[1-9]d{17}|[1-9]d{16}x$/, //身份证 'call' :/(^(d{3,4}-)?d{7,8})$|(1[3,4,5,8][0-9]{9})/ //手机号 & 电话 }; function fyes(){ o.removeClass('no_verify'); if(yfn)yfn() return o.val(); } function fno(){ o.addClass('no_verify'); if(nfn)nfn() console.log('错误信息:'+attr) return false; } function go(){ if(attr && typeof attr !='number'){ if(o.val() && re[attr].test(o.val())){ return fyes() }else{ return fno() } }else if(attr && typeof attr =='number'){ if(o.val().length>attr){ return fyes() }else{ return fno() } }else if(!attr){ if(o.val()){ return fyes() }else{ return fno() } } }; if(run){ return go() } //run=true 验证一下表单 o.blur(go); };
试用方法 ://testInput( JQ对象 , '验证属性 || 不传值验证表单不为空 || 数字 验证字节不少于这个数' , '验证成功fn' , '失败fn' , 要不要自执行)
(最后一个值为 false :验证成功返回值,不成功会返回false; 这里还会对表单加一个class样式:no_verify)
var f2=testInput($('#phone'), 'call','','',false) //验证是不是电话或手机
var f5=testInput($('#userName'), '','','',false) //表单不为空
var f12=testInput($('#reason'),20,'','',false) //表单不小于20个