通用验证脚本

ContractedBlock.gif ExpandedBlockStart.gif 验证脚本
  1ExpandedBlockStart.gifContractedBlock.gif/**//****************************************
  2InBlock.gif  * Create by Oceanchip 2008-3-31
  3ExpandedBlockEnd.gif  ***************************************/

  4None.gif  
  5ExpandedBlockStart.gifContractedBlock.gif/**//*****************************
  6InBlock.gif * namespace:System
  7ExpandedBlockEnd.gif ******************************/
 
  8None.gifif(!window.System)
  9ExpandedBlockStart.gifContractedBlock.gif    window.System = dot.gif{};
 10None.gif    
 11ExpandedBlockStart.gifContractedBlock.gif/**//************************************
 12InBlock.gif *  function System.createDeletegate
 13ExpandedBlockEnd.gif ************************************/

 14None.gifSystem.createDeletegate=function(instance,method)
 15ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 16InBlock.gif    ///<summary> Creates a delegate function that executes the specified method in the correct context.</summary>
 17InBlock.gif    ///<param name="instance">The instance whose method should be executed.</param>
 18InBlock.gif    ///<param name="method">The method to execute.</param>
 19InBlock.gif    ///<returns>The delegate function.</returns>
 20InBlock.gif    return function()
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 22InBlock.gif        return method.apply(instance,arguments);
 23ExpandedSubBlockEnd.gif    }

 24ExpandedBlockEnd.gif}

 25ExpandedBlockStart.gifContractedBlock.gif/**//************************************
 26InBlock.gif *  function System.merge
 27ExpandedBlockEnd.gif ************************************/

 28None.gif System.merge = function(destination,source,preventNew)
 29ExpandedBlockStart.gifContractedBlock.gif dot.gif{
 30InBlock.gif    ///<summary>Merges properties from a source object into a destination object.</summary>
 31InBlock.gif    ///<param name="destination">The destination object.</param>
 32InBlock.gif    ///<param name="source">The source object.</param>
 33InBlock.gif    ///<param name="preventNew">Indicates whether or not new properties are prevented from being added to the destination object.</param>
 34InBlock.gif    
 35InBlock.gif    var root = destination;
 36InBlock.gif    forvar i in source)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 38InBlock.gif        var s = source[i],d;
 39InBlock.gif        var properties = i.split(".");
 40InBlock.gif        var count = properties.length;
 41InBlock.gif        
 42InBlock.gif        for(var j = 0; j < count; j++)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            i =properties[j];
 45InBlock.gif            d = destination[i];
 46InBlock.gif            if(d)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 48InBlock.gif                if(typeof d == "object")
 49InBlock.gif                    destination = d;
 50ExpandedSubBlockEnd.gif            }

 51InBlock.gif            else if (j > 0 && j < count -1)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53ExpandedSubBlockStart.gifContractedSubBlock.gif                var obj = dot.gif{};
 54InBlock.gif                obj[properties.slice(j,count).join(".")]=s;
 55InBlock.gif                s = obj;
 56InBlock.gif                break;
 57ExpandedSubBlockEnd.gif            }

 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif        
 60InBlock.gif        if(d && typeof(d) == "object" && typeof(s) == "object")
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            this.merge(d,s,false);
 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif        else if (preventNew && count<=1 && typeof(d) == "undefined")
 65InBlock.gif            throw new Error("Undefined property:"+i);
 66InBlock.gif        else
 67InBlock.gif            destination[i] = s;
 68InBlock.gif            
 69InBlock.gif        destination = root;
 70ExpandedSubBlockEnd.gif    }

 71ExpandedBlockEnd.gif }

 72ExpandedBlockStart.gifContractedBlock.gif /**//************************************
 73InBlock.gif *  function System.extend
 74ExpandedBlockEnd.gif ************************************/

 75None.gif System.extend = function(baseClass,derivedClass,deriveMembers)
 76ExpandedBlockStart.gifContractedBlock.gif dot.gif{
 77InBlock.gif    ///<summary>Provides support for class inheritance.</summary>
 78InBlock.gif    ///<param name="baseClass">The base class.</param>
 79InBlock.gif    ///<param name="derivedClass">The derived class.</param>
 80InBlock.gif    ///<param name="derivedMembers">The members to add to the derived class and override in the base class.</param>
 81InBlock.gif    
 82ExpandedSubBlockStart.gifContractedSubBlock.gif    var F = function()dot.gif{};
 83InBlock.gif    F.prototype = baseClass.prototype;
 84InBlock.gif    derivedClass.prototype = new F();
 85InBlock.gif    derivedClass.prototype.constructor = derivedClass;
 86InBlock.gif    derivedClass.base = baseClass.prototype;
 87InBlock.gif    
 88InBlock.gif    if(baseClass.prototype.constructor == Object.prototype.constructor)
 89InBlock.gif        baseClass.prototype.constructor = baseClass;
 90InBlock.gif    
 91InBlock.gif    //Note:IE will not enumerate derived members that exist in the Object
 92InBlock.gif    //prototype(e.g. toString,valueOf). If overriding these members
 93InBlock.gif    //is necessary,search for "_IEEnumFix" for one possible solution.    
 94InBlock.gif    if(deriveMembers)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 96InBlock.gif        for(var i  in deriveMembers)
 97InBlock.gif            derivedClass.prototype[i] = deriveMembers[i];
 98ExpandedSubBlockEnd.gif    }

 99ExpandedBlockEnd.gif }

100None.gif 
101ExpandedBlockStart.gifContractedBlock.gif /**//************************************
102InBlock.gif *  function System.parseBoolean
103ExpandedBlockEnd.gif ************************************/

104None.gif System.parseBoolean = function(value)
105ExpandedBlockStart.gifContractedBlock.gif dot.gif{
106InBlock.gif    ///<summary>Parse a boolean from the specified value.</summary>
107InBlock.gif    ///<param name="value">The value to parse.</param>
108InBlock.gif    ///<returns>True if the specified value is parsed as true.</returns>
109InBlock.gif    
110InBlock.gif    return (typeof(value)=="string"? (value.toLowerCase() == "true"):Boolean(value);
111ExpandedBlockEnd.gif }

112None.gif 
113ExpandedBlockStart.gifContractedBlock.gif /**//************************************
114InBlock.gif *  function System.formatString
115ExpandedBlockEnd.gif ************************************/

116None.gif System.formatString = function(value)
117ExpandedBlockStart.gifContractedBlock.gif dot.gif{
118InBlock.gif    ///<summary>FOrmats a string.</summary>
119InBlock.gif    ///<param name = "value">The string to format.</param>
120InBlock.gif    ///<returns>The formatted string.</returns>
121InBlock.gif    
122InBlock.gif    for(var i = 1,j = arguments.length;i<j; i++)
123ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
124InBlock.gif        value = value.replace("{"+ (i - 1+ "}",arguments[i]);
125ExpandedSubBlockEnd.gif    }

126InBlock.gif    return value;
127ExpandedBlockEnd.gif }

128None.gif System.trim = function(value)
129ExpandedBlockStart.gifContractedBlock.gif dot.gif{
130InBlock.gif        return value.replace(/(^\s*)|(\s*$)/g, "");
131ExpandedBlockEnd.gif }

132ExpandedBlockStart.gifContractedBlock.gif /**//*************************************
133InBlock.gif *  function System.getUniqueId
134ExpandedBlockEnd.gif ************************************/

135None.gif System.getUniqueId = function(prefix)
136ExpandedBlockStart.gifContractedBlock.gif dot.gif{
137InBlock.gif    ///<summary>Get a random unique identifer.</summary>
138InBlock.gif    ///<param name="prefix">The prefix to prepend to the generated identifier.</param>
139InBlock.gif    ///<returns> The unique identifier.</returns>
140InBlock.gif    
141InBlock.gif    return prefix + Math.random().toString().substring(2);
142ExpandedBlockEnd.gif }

143None.gif  
144ExpandedBlockStart.gifContractedBlock.gif /**//************************************
145InBlock.gif *  function System.Object
146ExpandedBlockEnd.gif ************************************/

147None.gifSystem.Object = function()
148ExpandedBlockStart.gifContractedBlock.gif dot.gif{
149InBlock.gif    ///<summary>Provides a common base class with support for options and events.</summary>
150InBlock.gif    
151ExpandedSubBlockStart.gifContractedSubBlock.gif    this.options = dot.gif{};
152ExpandedSubBlockStart.gifContractedSubBlock.gif    this.eventHandlers = dot.gif{};
153ExpandedBlockEnd.gif }

154None.gif 
155None.gif System.Object.prototype = 
156ExpandedBlockStart.gifContractedBlock.gif dot.gif{
157InBlock.gif    setOptions: function(options)
158ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
159InBlock.gif        ///<summary>Merges the specified options with existing options.</summary>
160InBlock.gif        ///<param name="options">The options to merge.</summary>
161InBlock.gif        
162InBlock.gif        System.merge(this.options,options,true);
163ExpandedSubBlockEnd.gif    }
,
164InBlock.gif    
165InBlock.gif    addEventListener : function(name , handler)
166ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
167InBlock.gif        ///<summary>Adds an event handler to the list of handlers to be called when the specified event is fired.</summary>
168InBlock.gif        ///<param name="name">The event name.</param>
169InBlock.gif        ///<param name="handler">The event handler to add.</param>
170InBlock.gif        ///<returns>The identifying token(i.e. index) for the added event handler.</returns>
171InBlock.gif        
172InBlock.gif        var handlers = this.eventHandlers[name];
173InBlock.gif        
174InBlock.gif        if(!handlers)
175InBlock.gif            this.eventHandlers[name] = handlers = [];
176InBlock.gif        
177InBlock.gif        var token = handlers.length;
178InBlock.gif        handlers[token] = handler;
179InBlock.gif        return token;
180ExpandedSubBlockEnd.gif    }
,
181InBlock.gif    
182InBlock.gif    removeEventListener: function(name,handlerOrToken)
183ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
184InBlock.gif        ///<summary>Removes the first matching event handler from the list of handlers to be called when the specified event is fired.</summary>
185InBlock.gif        ///<param name="name">The event name.</param>
186InBlock.gif        ///<param name="handlerOrToken">The event handler or indentiing token to remove.</param>
187InBlock.gif        
188InBlock.gif        if(typeof(handlerOrToken) == "function")
189ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
190InBlock.gif            var handlers = this.eventHandlers[name];
191InBlock.gif            
192InBlock.gif            if(handlers)
193ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
194InBlock.gif                for(var i = 0, j = handlers.length; i < j; i++)
195ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
196InBlock.gif                    if(handlers[i] == handlerOrToken)
197InBlock.gif                        break;
198ExpandedSubBlockEnd.gif                }

199InBlock.gif                
200InBlock.gif                handlers.splice(i,1);
201ExpandedSubBlockEnd.gif            }

202ExpandedSubBlockEnd.gif        }

203ExpandedSubBlockEnd.gif    }
,
204InBlock.gif    
205InBlock.gif    fireEvent: function(name, e)
206ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
207InBlock.gif        ///<summary> Fires the specified event and calls each listening handler.</summary>
208InBlock.gif        ///<param name="name">The name of the event to fire.</param>
209InBlock.gif        ///<param name="e">The event arguments to pass to each handler.</param>
210InBlock.gif        
211InBlock.gif        var handlers = this.eventHandlers[name];
212InBlock.gif        
213InBlock.gif        if(handlers)
214InBlock.gif            forvar i = 0, j = handlers.length; i < j; i++)
215InBlock.gif                handlers[i](this,e);
216ExpandedSubBlockEnd.gif    }
,
217InBlock.gif    
218InBlock.gif    dispose: function()
219ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
220InBlock.gif        ///<summary> Release the object from memory.</summary>
221InBlock.gif        
222InBlock.gif        this.options = null;
223InBlock.gif        this.eventHandlers = null;
224ExpandedSubBlockEnd.gif    }

225ExpandedBlockEnd.gif }

226ExpandedBlockStart.gifContractedBlock.gif /**//************************************
227InBlock.gif *  function System.Validate
228ExpandedBlockEnd.gif ************************************/

229None.gifSystem.Validate = function(options)
230ExpandedBlockStart.gifContractedBlock.gifdot.gif{
231InBlock.gif    ///<summary>Provider support from Validate check</summary>
232InBlock.gif    ///<param name="options">The options from Validate.</param>
233InBlock.gif    
234InBlock.gif    System.Validate.base.constructor.call(this);
235InBlock.gif    
236InBlock.gif    this.reg = null;
237InBlock.gif    this.msg = '';
238InBlock.gif    
239ExpandedBlockEnd.gif}

240None.gifSystem.extend(System.Object,System.Validate,
241ExpandedBlockStart.gifContractedBlock.gifdot.gif{
242InBlock.gif    check:function(val)
243ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
244InBlock.gif        if(this.reg.test(val))
245InBlock.gif            return true;
246InBlock.gif        return false;
247ExpandedSubBlockEnd.gif    }
,
248InBlock.gif    regex:function(val,reg)
249ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
250InBlock.gif        if(reg.test(val))
251ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
252InBlock.gif            this.msg = "";
253InBlock.gif            return true;
254ExpandedSubBlockEnd.gif        }

255InBlock.gif        return false;
256ExpandedSubBlockEnd.gif    }
,
257InBlock.gif    //是否数字
258InBlock.gif    isInteger:function(val)
259ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
260InBlock.gif        if(System.trim(val) == '')return true;
261InBlock.gif        this.reg = /^[-+]?\d*$/;
262InBlock.gif        this.msg='请输入阿拉伯数字\r\n'
263InBlock.gif        return this.check(val);
264ExpandedSubBlockEnd.gif    }
,
265InBlock.gif    //是否浮点数
266InBlock.gif    isDouble:function(val)
267ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
268InBlock.gif        if(System.trim(val) == '')return true;
269InBlock.gif        this.reg =/^[-+]?\d+(\.\d+)?$/;
270InBlock.gif        this.msg='请输入数字,如3.14\r\n';
271InBlock.gif        return this.check(val);
272ExpandedSubBlockEnd.gif    }
,
273InBlock.gif    //电子邮件
274InBlock.gif    isEmail:function(val)
275ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
276InBlock.gif        if(System.trim(val) == '')return true;
277InBlock.gif        this.reg = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
278InBlock.gif        this.msg ="请输入合法的email地址\r\n";
279InBlock.gif        return this.check(val);
280ExpandedSubBlockEnd.gif    }
,
281InBlock.gif    //货币
282InBlock.gif    isCurrency:function(val)
283ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
284InBlock.gif        if(System.trim(val) == '')return true;
285InBlock.gif        this.reg = /^[$¥]?(\d{1,3}(\,d{3})*|(\d+))(\.\d{2})?$/;
286InBlock.gif        this.msg="请输入金额,或以美元或人民币符合开头的数字,小数保留两位,如:¥1,000.00\r\n";
287InBlock.gif        return this.check(val);
288ExpandedSubBlockEnd.gif    }
,
289InBlock.gif    isString:function(val)
290ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
291InBlock.gif        if(System.trim(val) == '')return true;
292InBlock.gif        this.reg=/^\w+$/;
293InBlock.gif        this.msg="请输入规则字符串\r\n";
294InBlock.gif        return this.check(val);
295ExpandedSubBlockEnd.gif    }
,    
296InBlock.gif    isStringInteger:function(val)
297ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
298InBlock.gif        if(System.trim(val) == '')return true;
299InBlock.gif        this.reg=/^[a-zA-Z][0-9]+$/;
300InBlock.gif        this.msg = "请输入首位是字母其他未数字的字符串\r\n";
301InBlock.gif        return this.check(val);
302ExpandedSubBlockEnd.gif    }
,
303InBlock.gif    isStringOrInteger:function(val)
304ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
305InBlock.gif        if(System.trim(val) == '')return true;
306InBlock.gif        this.reg=/^[a-zA-Z0-9]+$/;
307InBlock.gif        this.msg = "请输入字母或数字\r\n";
308InBlock.gif        return this.check(val);
309ExpandedSubBlockEnd.gif    }
,
310InBlock.gif    isZip:function(val)
311ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
312InBlock.gif        if(System.trim(val) == '')return true;
313InBlock.gif        this.reg=/^\d{6}$/;
314InBlock.gif        this.msg="请输入邮政编码,如:310000\r\n";
315InBlock.gif        return this.check(val);
316ExpandedSubBlockEnd.gif    }
,
317InBlock.gif    isUnsignInteger:function(val)
318ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
319InBlock.gif        if(System.trim(val) == '')return true;
320InBlock.gif        this.reg = /^\d*$/;
321InBlock.gif        this.msg = "请输入阿拉伯数字\r\n";
322InBlock.gif        return this.check(val);
323ExpandedSubBlockEnd.gif    }
,
324InBlock.gif    isLetter:function(val)
325ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
326InBlock.gif        if(System.trim(val) == '')return true;
327InBlock.gif        this.reg = /^[a-zA-Z]+$/;
328InBlock.gif        this.msg = "请输入英文字母\r\n";
329InBlock.gif        return this.check(val);
330ExpandedSubBlockEnd.gif    }
,
331InBlock.gif    isChinese:function(val)
332ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
333InBlock.gif        if(System.trim(val) == '')return true;
334InBlock.gif        this.reg=/^[\w\u00ff-\ufffd]+$/;
335InBlock.gif        this.msg = "请输入中文\r\n";
336InBlock.gif        return this.check(val);
337ExpandedSubBlockEnd.gif    }
,
338InBlock.gif    maxLength:function(maxLen,val)
339ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
340InBlock.gif        if(maxLen==0)return true;
341InBlock.gif        var len =System.trim(val).length;//this.strLength(val);
342InBlock.gif        if(len<=parseInt(maxLen))
343InBlock.gif            return true;
344InBlock.gif        this.msg = "输入的长度是实际"+len+",不能超过"+maxLen+"位\r\n";
345InBlock.gif        return false;
346ExpandedSubBlockEnd.gif    }
,
347InBlock.gif    minLength:function(minLen,val)
348ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
349InBlock.gif        if(minLen == 0)return true;
350InBlock.gif        var len = System.trim(val).length;
351InBlock.gif        if(len >= minLen)
352InBlock.gif            return true;
353InBlock.gif        this.msg = "输入的长度实际为"+len+",不能小于"+minLen+"位\r\n";
354InBlock.gif        return false;
355ExpandedSubBlockEnd.gif    }
,
356InBlock.gif    ///去字符长度,中文一个字占两个字符长度
357InBlock.gif    strLength:function(val)
358ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
359InBlock.gif        var l = val.length;
360InBlock.gif        var n = l;
361InBlock.gif        for(var i = 0;i<l;i++)
362ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
363InBlock.gif            if(val.charcodeat(i)<0 || val.charcodeat(i)>255)n++;
364ExpandedSubBlockEnd.gif        }

365InBlock.gif        return n;
366ExpandedSubBlockEnd.gif    }
,
367InBlock.gif    isDate:function(val)
368ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
369InBlock.gif        if(val == ""return true;
370InBlock.gif        if(val.indexOf(':'< 0)
371InBlock.gif            val += ' 0:00:00';
372ExpandedSubBlockStart.gifContractedSubBlock.gif        var r = val.match(/^(\d{1,4})(-|\/)(\ddot.gif{1,2})\2(\ddot.gif{1,2}) (\ddot.gif{1,2}):(\ddot.gif{1,2}):(\ddot.gif{1,2})$/);
373InBlock.gif        this.msg = "你输入的日期格式不正确\r\n";
374InBlock.gif        if(r == null)    return false
375InBlock.gif        var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
376InBlock.gif        return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
377ExpandedSubBlockEnd.gif    }
    
378ExpandedBlockEnd.gif}
);
379None.gif
380ExpandedBlockStart.gifContractedBlock.gif/**//*
381InBlock.gif * 参数说明:
382InBlock.gif *       btnid:提交按钮  
383InBlock.gif *       focus是不是在用户离开控件就开始验证,true为是,false为在提交的时候才开始验证 
384InBlock.gif *  页面设置说明:
385InBlock.gif *       regType:{
386InBlock.gif *              integer: 整型
387InBlock.gif *              float:     浮点型 
388InBlock.gif *              date:     日期格式
389InBlock.gif *              email:    电子游戏
390InBlock.gif *              letter:    26个英文字符   
391InBlock.gif *              chinese: 只允许中文
392InBlock.gif *              zip:       邮编
393InBlock.gif *              number:数字(0-9)
394InBlock.gif *              string:   常规字符
395InBlock.gif *              stringorinteger:数字与字符(a-Z,0-9)
396InBlock.gif *              stringInteger:首位字符,其余为数字
397InBlock.gif *              curreny:  货币
398InBlock.gif *              reg:      正则表达式,并且需提供  regex 属性
399InBlock.gif *      } 
400InBlock.gif *      maxLength:最大长度
401InBlock.gif *      minLength:最小长度
402InBlock.gif *      minValue:最小值
403InBlock.gif *      maxValue:最大值,minValue,maxValue只对regType值为integer与float有效
404InBlock.gif *      notnull:不能为空(如果可以为空,则不用设置
405InBlock.gif *      errorMsg:错误提示 
406InBlock.gif * 页面应用举例:
407InBlock.gif *      <input regtype='integer' notnull maxLength="16" minLength="6" />
408InBlock.gif *       
409InBlock.gif *调用说明:
410InBlock.gif * var formCheck = new System.FormCheck("btnid",{focus:true,formid:"form1" });
411InBlock.gif *   
412ExpandedBlockEnd.gif */

413ExpandedBlockStart.gifContractedBlock.gif /**//************************************
414InBlock.gif *  function System.FormCheck
415ExpandedBlockEnd.gif ************************************/
 
416None.gifSystem.FormCheck = function(btnid,options)
417ExpandedBlockStart.gifContractedBlock.gifdot.gif{
418InBlock.gif    ///<summary>Provide support from form validate</summary>
419InBlock.gif   ///<param name="btnid">The Button's identifier,the button which sumit the form to server </param>
420InBlock.gif   
421InBlock.gif    System.FormCheck.base.constructor.call(this);
422InBlock.gif    
423InBlock.gif    System.merge(this.options,
424ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
425InBlock.gif        formid:'',
426InBlock.gif        focus:false
427ExpandedSubBlockEnd.gif    }
);
428InBlock.gif    this.btnid = btnid;
429InBlock.gif    
430InBlock.gif    this.setOptions(options);
431InBlock.gif    this.addEventListener("onload",System.createDeletegate(this,this.onLoad));
432InBlock.gif    window.setTimeout(System.createDeletegate(thisthis.checkLoadStatus),100);
433InBlock.gif    //this.init();
434ExpandedBlockEnd.gif}

435None.gifSystem.extend(System.Validate,System.FormCheck,
436ExpandedBlockStart.gifContractedBlock.gifdot.gif{
437InBlock.gif    checkLoadStatus:function()
438ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
439InBlock.gif        if(document.readyState && document.readyState !='loaded' && document.readyState != 'complete')return;
441InBlock.gif
461InBlock.gif            window.setTimeout(System.createDeletegate(thisthis.checkLoadStatus),100);
462InBlock.gif            return;
463ExpandedSubBlockEnd.gif        }

464InBlock.gif        this.fireEvent("onload");
465ExpandedSubBlockEnd.gif    }
,
466InBlock.gif    onLoad:function()
467ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
468InBlock.gif        this.controls = [];
469InBlock.gif        var doc = null;
470InBlock.gif        if(typeof this.options.formid=='undefined' || this.options.formid=='')
471ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
472InBlock.gif            doc = document.compatMode == "CSS1Compat" ? document.documentElement : document.body;
473ExpandedSubBlockEnd.gif        }

474InBlock.gif        else
475ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
476InBlock.gif            doc = document.getElementById(this.options.formid);
477ExpandedSubBlockEnd.gif        }

478InBlock.gif        if(!doc)return;
479InBlock.gif        
480InBlock.gif        var inputs = doc.getElementsByTagName("INPUT");
481InBlock.gif        for(var i = 0;i<inputs.length;i++)
482ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
483InBlock.gif            if(inputs[i].type.toLowerCase() =='text')
484ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
485InBlock.gif                this.controls.push(inputs[i]);
486InBlock.gif                if(this.options.focus)
487ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
488InBlock.gif                    inputs[i].onblur = System.createDeletegate(this,this.Validate);
489ExpandedSubBlockEnd.gif                }

490ExpandedSubBlockEnd.gif            }

491ExpandedSubBlockEnd.gif        }

492InBlock.gif        var textArea = doc.getElementsByTagName("TEXTAREA");
493InBlock.gif        for(var i = 0;i<textArea.length;i++)
494ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
495InBlock.gif            this.controls.push(textArea[i]);
496InBlock.gif            if(this.options.focus)
497ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
498InBlock.gif                textArea[i].onblur = this.Validate;
499ExpandedSubBlockEnd.gif            }

500ExpandedSubBlockEnd.gif        }

501InBlock.gif        this.register(this.btnid);
502ExpandedSubBlockEnd.gif    }
,
503InBlock.gif    register:function(id)
504ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{      
505InBlock.gif      var ctrl = document.getElementById(id);
506InBlock.gif      if(!ctrl)
507InBlock.gif        throw new Error("控件ID为"+id的对象不存在);
508InBlock.gif        var _oldClick = ctrl.onclick;
509InBlock.gif        if(_oldClick)
510ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
511InBlock.gif            var obj = this;
512InBlock.gif            ctrl.onclick = function()
513ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
514InBlock.gif                var flag = obj.Validate();
515InBlock.gif               if(flag) 
516ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif
517InBlock.gif                    _oldClick(); 
518InBlock.gif                    return true;
519ExpandedSubBlockEnd.gif               }

520InBlock.gif               return false;
521ExpandedSubBlockEnd.gif            }

522ExpandedSubBlockEnd.gif        }

523InBlock.gif        else
524ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
525InBlock.gif            ctrl.onclick =System.createDeletegate(this,this.Validate);
526ExpandedSubBlockEnd.gif        }

527InBlock.gif       //ctrl.onclick =  System.createDeletegate(this,this.Validate);
528InBlock.gif      //ctrl.onclick = this.Validate; 
529InBlock.gif      //this.addEventListener("onclick",System.createDeletegate(ctrl,this.Validate));
530ExpandedSubBlockEnd.gif    }
,
531InBlock.gif   //判断控件是否可见,style.display,visibility 
532InBlock.gif   isHidden:function(ctrl)
533ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
534InBlock.gif        if(ctrl.display == 'none' && ctrl.style.visibility=='hidden')
535ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
536InBlock.gif            return true;
537ExpandedSubBlockEnd.gif        }

538InBlock.gif        ctrl = ctrl.parentNode;
539InBlock.gif        while(ctrl.tagName != 'BODY')
540ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
541InBlock.gif            if(ctrl.style.display == 'none' || ctrl.style.visibility=='hidden')
542ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
543InBlock.gif                return true;
544ExpandedSubBlockEnd.gif            }

545InBlock.gif            ctrl = ctrl.parentNode;
546ExpandedSubBlockEnd.gif        }

547InBlock.gif        return false;
548ExpandedSubBlockEnd.gif   }

549InBlock.gif    Validate:function()
550ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{        
551InBlock.gif        var sign = true;
552InBlock.gif        var msg = "";
553InBlock.gif        var firstNode = null;
554InBlock.gif        for(var i =0;i<this.controls.length;i++)
555ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
556InBlock.gif            if(this.isHidden(this.controls[i]))continue;
557InBlock.gif            var type = this.controls[i].getAttribute("regType")||"";
558InBlock.gif            var value = this.controls[i].value||"";
559InBlock.gif            value = System.trim(value);
560InBlock.gif            var falg;
561InBlock.gif            switch(type.toLowerCase())
562ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
563InBlock.gif                case "integer":                     
564InBlock.gif                    flag = this.isInteger(value);
565InBlock.gif                    if(flag && value != "")
566ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
567InBlock.gif                        var minValue = this.controls[i].getAttribute("minValue")||"";;
568InBlock.gif                        var maxValue = this.controls[i].getAttribute("maxValue")||"";
569InBlock.gif                       if(minValue!="")
570ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
571InBlock.gif                            if(parseInt(minValue) >parseInt(value))
572ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
573InBlock.gif                                this.msg = "值不能小于"+minValue+'\r\n';
574InBlock.gif                                flag = false;
575ExpandedSubBlockEnd.gif                            }

576ExpandedSubBlockEnd.gif                        }

577InBlock.gif                        if(flag && maxValue!="")
578ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
579InBlock.gif                            if(parseInt(maxValue)<parseInt(value))
580ExpandedSubBlockStart.gifContractedSubBlock.gif                           dot.gif{                              
581InBlock.gif                                this.msg ="值不能大于"+maxValue+'\r\n';
582InBlock.gif                                flag = false;
583ExpandedSubBlockEnd.gif                           }
 
584ExpandedSubBlockEnd.gif                        }

585ExpandedSubBlockEnd.gif                    }

586InBlock.gif                    break;
587InBlock.gif                case "float":
588InBlock.gif                    flag = this.isDouble(value);
589InBlock.gif                    if(flag && value != "")
590ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
591InBlock.gif                        var minValue = this.controls[i].getAttribute("minValue")||"";;
592InBlock.gif                        var maxValue = this.controls[i].getAttribute("maxValue")||"";
593InBlock.gif                       if(minValue!="")
594ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
595InBlock.gif                            if(parseFloat(minValue) >parseFloat(value))
596ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
597InBlock.gif                                this.msg = "值不能小于"+minValue;
598InBlock.gif                                flag = false;
599ExpandedSubBlockEnd.gif                            }

600ExpandedSubBlockEnd.gif                        }

601InBlock.gif                        if(flag && maxValue!="")
602ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
603InBlock.gif                            if(parseFloat(maxValue)<parseFloat(value))
604ExpandedSubBlockStart.gifContractedSubBlock.gif                           dot.gif{
605InBlock.gif                                this.msg ="值不能大于"+maxValue;
606InBlock.gif                                flag = false;
607ExpandedSubBlockEnd.gif                           }
 
608ExpandedSubBlockEnd.gif                        }

609ExpandedSubBlockEnd.gif                    }

610InBlock.gif                    break;
611InBlock.gif                case "date":
612InBlock.gif                    flag = this.isDate(value);
613InBlock.gif                    break;
614InBlock.gif                case "string":
615InBlock.gif                    flag = this.isString(value);
616InBlock.gif                    break;
617InBlock.gif                case "stringorinteger":
618InBlock.gif                    flag = this.isStringOrInteger(value);
619InBlock.gif                    break;
620InBlock.gif                case "stringinteger":
621InBlock.gif                    flag = this.isStringInteger(value);
622InBlock.gif                    break;
623InBlock.gif                case "letter":
624InBlock.gif                    flag = this.isLetter(value);
625InBlock.gif                    break;
626InBlock.gif                case "zip":
627InBlock.gif                    flag = this.isZip(value);
628InBlock.gif                    break;
629InBlock.gif                case "email":
630InBlock.gif                    flag = this.isEmail(value);
631InBlock.gif                    break;
632InBlock.gif                case "chinese":
633InBlock.gif                    flag = this.isChinese(value);
634InBlock.gif                    break;
635InBlock.gif                case "number":
636InBlock.gif                    flag = this.isUnsignInteger(value);
637InBlock.gif                    break;
638InBlock.gif                case "curreny":
639InBlock.gif                    flag = this.isCurreny(value);
640InBlock.gif                    break;
641InBlock.gif                case "reg":
642InBlock.gif                    var reg = this.controls[i].getAttribute("regex");
643InBlock.gif                    if(reg)
644InBlock.gif                        flag = this.regex(value,reg);
645InBlock.gif                    flag = true;
646InBlock.gif                    break;
647ExpandedSubBlockEnd.gif            }
 
648InBlock.gif            if(sign)sign = flag;           
649InBlock.gif            if(sign == false)
650ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
651InBlock.gif                if(firstNode == null)
652ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
653InBlock.gif                    firstNode = this.controls[i];
654ExpandedSubBlockEnd.gif                }

655InBlock.gif                if(this.controls[i].getAttribute("errorMsg"))
656InBlock.gif                    msg +=this.controls[i].getAttribute("errMsg")+"\r\n";
657InBlock.gif                else if(this.msg != '')
658ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
659InBlock.gif                    msg +=this.msg;     
660ExpandedSubBlockEnd.gif                }

661InBlock.gif               this.msg = ""
662InBlock.gif                if(this.options.focus)
663ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
664InBlock.gif                    break;
665ExpandedSubBlockEnd.gif                }
                
666ExpandedSubBlockEnd.gif            }

667InBlock.gif            else 
668ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
669InBlock.gif                var maxLength = this.controls[i].getAttribute("maxLength");
670InBlock.gif                if(maxLength)
671ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
672InBlock.gif                    flag = this.maxLength(maxLength,value);
673ExpandedSubBlockEnd.gif                }

674InBlock.gif               sign = flag; 
675InBlock.gif                var minLen = this.controls[i].getAttribute("minLength");
676InBlock.gif                if(minLen)
677ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
678InBlock.gif                    flag = this.minLength(minLen,value);
679InBlock.gif                    msg +=this.msg+"\r\n";
680ExpandedSubBlockEnd.gif                }

681InBlock.gif                if(sign)sign = flag;
682InBlock.gif                 if(this.controls[i].getAttribute("notnull"!=null)
683ExpandedSubBlockStart.gifContractedSubBlock.gif                 dot.gif{
684InBlock.gif                    if(System.trim(value) == "")
685ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
686InBlock.gif                        if(firstNode == null)
687ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
688InBlock.gif                            firstNode = this.controls[i];
689ExpandedSubBlockEnd.gif                        }

690InBlock.gif                        flag =false;
691InBlock.gif                        msg = "内容不能为空";
692ExpandedSubBlockEnd.gif                    }

693ExpandedSubBlockEnd.gif                }

694InBlock.gif                if(sign)sign = flag;
695InBlock.gif                if(this.options.focus && sign==false)
696ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
697InBlock.gif                    break;
698ExpandedSubBlockEnd.gif                }

699ExpandedSubBlockEnd.gif            }

700ExpandedSubBlockEnd.gif        }

701InBlock.gif        if(sign == false)
702ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
703InBlock.gif            alert(msg);
704InBlock.gif            firstNode.select();
705InBlock.gif            firstNode.focus();            
706ExpandedSubBlockEnd.gif        }

707InBlock.gif        delete msg;
708InBlock.gif        event.returnValue=sign;
709InBlock.gif        return sign;
710InBlock.gif        this.firEvent('validateComplete');
711ExpandedSubBlockEnd.gif    }

712ExpandedBlockEnd.gif}
);

由于没有太多的时间,并且代码中已经有比较详细的说明,所以就不在过多解释了,由于脚本刚完成不久,所以可能还存在问题。
以后有时间的话,会解释脚本中类的继承的实现。
脚本 下载

转载于:https://www.cnblogs.com/Oceanchip/archive/2008/03/31/1131878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值