初步實現。
??1?<!--
??2?????/**********************************************************
??3??????*?validatorUtil.js
??4??????*?@author?lightning
??5??????*?@version?0.1
??6??????*?使用javascript的prototype特性對String等對象進行擴展
??7??????*?建立一個輸入交驗的基本工具。由于實現中大量的使用了正則表達式,
??8??????*?需要修改源碼的朋友請先自己復習一下javascript的正則表達式
??9??????*?
?10??????**********************************************************/
?11?
?12?????/**
?13??????*?此處選擇對String進行擴展主要是因為javascript與html,jsp等頁面
?14??????*?進行交互的過程中,主要的數據類型是String(javascript取得的頁面數據,基本上默認都是String類型)。
?15??????*?即使要使用別的數據類型,也可以和String很方便的進行轉化。
?16??????*/
?17?????String.prototype.trim?=?trim;
?18?????String.prototype.isEmpty?=isEmpty;
?19?????String.prototype.isEmail=isEmail;
?20?????String.prototype.isIP=isIP;
?21?????String.prototype.isNumber=isNumber;
?22?????String.prototype.isEnglish=isEnglish;
?23?????String.prototype.isChinese=isChinese;
?24?????String.prototype.hasChinese=hasChinese;
?25?????String.prototype.isIntBetween=isIntBetween;
?26?????String.prototype.byteLength=byteLength;
?27?????
?28?????/**
?29??????*?$
?30??????*?@param?{String}?id
?31??????*?@return?{Element}?返回具有指定id的Element對象
?32??????*/
?33?????function?$(id)?{
?34?????????var?element;
?35?????????if?(typeof?id?==?'string'){
?36???????????element?=?document.getElementById(id);
?37?????????}
?38???????????
?39?//每取得一個element都對其進行這樣子的方法添加開銷可能會很大,不太現實??????????
?40?//??????????element.trim?=?trim;
?41?//??????????element.isEmpty?=isEmpty;
?42?//??????????element.isEmail=isEmail;
?43?//??????????element.isIP=isIP;
?44?//??????????element.isNumber=isNumber;
?45?//??????????element.isEnglish=isEnglish;
?46?//??????????element.isChinese=isChinese;
?47?//??????????element.hasChinese=hasChinese;
?48?//??????????element.isIntBetween=isIntBetween;
?49?//??????????element.byteLength=byteLength;
?50??????
?51?????????return?element;
?52?????}
?53?????
?54?????/**
?55??????*?$$
?56??????*?@param?{String}?id?
?57??????*?@return?{String}?id等于param的element的值
?58??????*/
?59?????function?$$(id)?{
?60?????????var?elementValue="";
?61?????????if?(typeof?id?==?'string'){
?62???????????elementValue?=?document.getElementById(id).value;
?63?????????}
?64?????????return?elementValue;
?65?????}
?66?????
?67?????/**
?68??????*?$A
?69??????*?<p>
?70??????*?????此方法最基本的使用是方便地取得指定id的element的指定attribute。
?71??????*??可以對此進行一些擴展使用。
?72??????*??如:在element中增加自定義的attribute--"errorMsg",
?73??????*?????用于保存一些固定的提示信息(可以和一些框架標簽結合使用實現提示信息的國際化)
?74??????*?????這樣就可以使用javascript方便地顯示這些信息
?75??????*?<p>
?76??????*?@param?{String}?id?element的id值
?77??????*?@param?{String}?attName?所要取得的attribute的名稱
?78??????*?@return?Attribute?返回所指定的Attribute的內容
?79??????*/
?80?????function?$A(?id,?attName?){
?81?????????return?document.getElementById(id).getAttribute(attName);
?82?????}
?83?????
?84?????/**
?85??????*?$N
?86??????*?@param?{String}?elementName?所要取得的element的名稱
?87??????*?@return?{Element}?返回具有指定名稱的Element對象數組
?88??????*/
?89?????function?$N(elementName){
?90?????????return?document.getElementsByName(elementName);
?91?????}
?92?????
?93?????/**
?94??????*?$Tag
?95??????*?@param?{String}?tagName?所要取得的tag的名稱
?96??????*?@return?{Element}?返回具有指定tag名(element類型)的Element對象數組
?97??????*/
?98?????function?$Tag(tagName){
?99?????????return?document.getElementsByTagName(tagName);
100?????}
101?????
102?????/**
103??????*?trim
104??????*?@return?{String}?去掉頭尾空白符的字符串
105??????*/
106?????function?trim(){
107?????????return?this.replace(/(^\s*)|(\s*$)/g,?"");
108?????}
109??????/**
110???????*?isEmpty
111???????*?@return?{Boolean}?判斷對象的值是否為空
112???????*/
113?????function?isEmpty(){
114?????????if(this.trim()==""){
115?????????????return?true;
116?????????}
117?????????return?false;
118?????}
119?????/**
120??????*?isEmail
121??????*?@return?{Boolean}?對象的值是否符合email的格式
122??????*/
123?????function?isEmail(){
124?????????var?EmailReg=/\w+@(\w+\.)+[a-z]{2,3}/g;
125?????????if(this.trim().search(EmailReg)!=-1){
126?????????????return?true;
127?????????}
128?????????return?false;
129?????}
130?????/**
131??????*?isIP
132??????*?@return?{Boolean}?對象的值是否符合IP的格式
133??????*/
134?????function?isIP()?{
135?????????var?ipReg=/^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$/g;
136?????????if(this.trim().search(ipReg)!=-1){
137?????????????return?true;
138?????????}
139?????????return?false;
140?????}
141?????/**
142??????*?isNumber
143??????*?@return?{Boolean}?Element的類型是否為Number
144??????*/
145??????function?isNumber()?{
146??????????var?nuReg=/^\d+$/g;
147?????????if(this.trim().search(nuReg)!=-1){
148?????????????return?true;
149?????????}
150?????????return?false;
151??????}
152?????/**
153??????*?isEnglish
154??????*?@return?{Boolean}?Element的值是否為英文(全部是英文字母)
155??????*/
156??????function?isEnglish()?{
157?????????var?enReg=/^[A-Za-z]+$/g;
158?????????if(this.trim().search(enReg)!=-1){
159?????????????return?true;
160?????????}
161?????????return?false;
162??????}
163?????/**
164??????*?isChinese
165??????*?@return?{Boolean}?Element的值是否為中文(全部是中文字符)
166??????*/
167?????function?isChinese()?{
168?????????var?chReg=/^[\u0391-\uFFE5]+$/g;
169?????????if(this.trim().search(chReg)!=-1){
170?????????????return?true;
171?????????}
172?????????return?false;
173?????}
174?????/**
175??????*?hasChinese
176??????*?@return?{Boolean}?Element是否包含有中文字符
177??????*/
178?????function?hasChinese()?{
179?????????var?chReg=/[\u0391-\uFFE5]+/g;
180?????????if(this.trim().search(chReg)!=-1){
181?????????????return?true;
182?????????}
183?????????return?false;
184?????}
185?????/**
186??????*?numBetween
187??????*?@param?{Number}?max?
188??????*?@param?{Number}?min
189??????*?@return?{Boolean}?Element的值是否大于min小于max
190??????*/
191??????function?isIntBetween(max,min)?{
192??????????min?=?min?||?0;
193?????????max?=?max?||?Number.MAX_VALUE;
194?????????return?min?<=?parseInt(this)?&&?parseInt(this)?<=?max;
195??????}
196??????/**
197???????*?byteLength
198???????*?@return?number?InputElemen的值所含字節數(雙字節字符算兩個)
199???????*/
200???????function?byteLength()?{
201???????????return?this.replace(/[^\x00-\xff]/g,"**").length;
202???????}
203???????
204???????/***********************************
205????????*?下面的這幾個是從spring嘟嘟的嘟嘟老窩里取得的
206????????*?還沒有經過整理,只供參考。大家要用的話可以自己進行整理。
207????????*?和筆者的實現不同,嘟嘟的這些方法中使用最多的實現方式是
208????????*?typeof和constructor
209????????***********************************/
210?//????function?isAlien(a)?{
211?//??????return?isObject(a)?&&?typeof?a.constructor?!=?'function';
212?//????}
213?//????
214?//?????
215?//????
216?//????function?isArray(a)?{
217?//??????return?isObject(a)?&&?a.constructor?==?Array;
218?//????}
219?//????
220?//????function?isBoolean(a)?{
221?//??????return?typeof?a?==?'boolean';
222?//????}
223?//????
224?//????function?isEmpty(o)?{
225?//??????var?i,?v;
226?//??????if?(isObject(o))?{
227?//????????for?(i?in?o)?{
228?//??????????v?=?o[i];
229?//??????????if?(isUndefined(v)?&&?isFunction(v))?{
230?//????????????return?false;
231?//??????????}
232?//????????}
233?//??????}
234?//??????return?true;
235?//????}
236?//
237?//????function?isFunction(a)?{
238?//??????return?typeof?a?==?'function';
239?//????}
240?//????
241?//????function?isNull(a)?{
242?//??????return?typeof?a?==?'object'?&&?!a;
243?//????}
244?//????
245?//????function?isNumber(a)?{
246?//??????return?typeof?a?==?'number'?&&?isFinite(a);
247?//????}
248?//????
249?//????function?isObject(a)?{
250?//??????return?(a?&&?typeof?a?==?'object')?||?isFunction(a);
251?//????}
252?//????
253?//????function?isString(a)?{
254?//??????return?typeof?a?==?'string';
255?//????}
256?//????
257?//????function?isUndefined(a)?{
258?//??????return?typeof?a?==?'undefined';
259?//????}
260?//-->
posted on 2006-05-10 16:50
OO 閱讀(383)
評論(0) 編輯 收藏 所屬分類:
java相關的亂七八糟的東西