[RIA]field validation when onblur |
/* @author shaomin @desc 验证当前字段是否合法 @param 1) :需要做何校验,目前只有trim,strDigit,ffc三个;数组形式, 因为有可能有多种校验组合,可以不传此参数,默认是不为空校验 2) : 验证该字段的url;//可以为空,则不发到server端验证 @example $("input[name='page.id']").verify(["trim","strDigit"],ctx+"template/verify.action?name=");*/jQuery.fn.extend({ verify: function(fn,vUrl) { var verify = new Verify($(this)); $(this).blur(function(){ var i=0,fnLen = fn.length; fn = fn || ["trim"]; /* while(i<fnLen){ verify[fn[i]]($(this).val()) i++; } */ for(i=0;i<fnLen;i++){ if(verify[fn[i]]($(this).val())){ break; }; } if(typeof vUrl == "undefined" || !vUrl || $.trim(vUrl).length < 1) return ; vUrl += encodeURIComponent(field); verify.serverCheck(vUrl); }); } });var Verify= function(_dom){ this._dom = _dom; if(!_dom.parent().find("span.err").html()){ _dom.insertAfter("<span class='err'></span>"); }};//no whitespace Verify.prototype.trim = function(field){ if($.trim(field).length < 1){ this._dom.parent().find("span.err").html("该字段不能为空"); this._dom.parent().find("span.err").css({ 'display' : 'inline' }); return true; } this.checkout(); };// digit and charater onlyVerify.prototype.strDigit = function(field){ if(!field.match(/^[A-Za-z0-9]+$/ig)){ this._dom.parent().find("span.err").html("该字段只能为数字和字母!"); this._dom.parent().find("span.err").css({ 'display' : 'inline' }); return true; } this.checkout();};//Fully Formed Character 全角字Verify.prototype.ffc = function(field){ if(field.match(/[^\x00-\xff]/g)){ this._dom.parent().find("span.err").html("该字段不能为中文!"); _dom.parent().find("span.err").css({ 'display' : 'inline' }); return; } this.checkout();};Verify.prototype.checkout = function(){ var _dom = this._dom; _dom.parent().find("span.err").css({ 'display' : 'none' });};Verify.prototype.serverCheck = function(vUrl){ var _self = this; Ajax.request(vUrl,{ GET : {}, onSuccess: function(res){ _self._dom.parent().find("span.err").css({ 'display' : 'none' }); }, onError : function(res){ _self.error(res); } }); };Verify.prototype.error = function(res){ this._dom.parent().find("span.err").html(res.data); this._dom.parent().find("span.err").css({ 'display' : 'inline' }); };
|
|
|  .: 公告
|
« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| .: 我的分类(专题) |  .: 最新日志 .: 最新回复
| 
blog名称:up forever 日志总数:130 评论数量:274 留言数量:4 访问次数:1948954 建立时间:2006年5月25日 |
|  .: 留言板 |  .: 链接 |

|