OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / jquery / jquery.format.1.05.js
1 /*
2  *
3  * Copyright (c) 2010 C. F., Wong (<a href="http://cloudgen.w0ng.hk">Cloudgen Examplet Store</a>)
4  * Licensed under the MIT License:
5  * http://www.opensource.org/licenses/mit-license.php
6  *
7  */
8 /*
9  *
10  * Copyright (c) 2011 Cloudgen Wong (<a href="http://www.cloudgen.w0ng.hk">Cloudgen Wong</a>)
11  * Licensed under the MIT License:
12  * http://www.opensource.org/licenses/mit-license.php
13  *
14  */
15 // version 1.05 
16 // fix the problem of jQuery 1.5 when using .val() 
17 // fix the problem when precision has been set and the input start with decimal dot or comma ,e.g. precision set to 3 and input with ".1234"
18 var email={tldn:new RegExp("^[^\@]+\@[^\@]+\.(A[C-GL-OQ-UWXZ]|B[ABD-JM-OR-TVWYZ]|C[ACDF-IK-ORUVX-Z]|D[EJKMOZ]|E[CEGR-U]|F[I-KMOR]|G[ABD-IL-NP-UWY]|H[KMNRTU]|I[DEL-OQ-T]|J[EMOP]|K[EG-IMNPRWYZ]|L[A-CIKR-VY]|M[AC-EGHK-Z]|N[ACE-GILOPRUZ]|OM|P[AE-HKL-NR-TWY]|QA|R[EOSUW]|S[A-EG-ORT-VYZ]|T[CDF-HJ-PRTVWZ]|U[AGKMSYZ]|V[ACEGINU]|W[FS]|XN|Y[ETU]|Z[AMW]|AERO|ARPA|ASIA|BIZ|CAT|COM|COOP|EDU|GOV|INFO|INT|JOBS|MIL|MOBI|MUSEUM|NAME|NET|ORG|PRO|TEL|TRAVEL)$","i")};
19 (function($){
20   $.extend($.expr[":"],{
21     regex:function(d,a,c){
22       var e=new RegExp(c[3],"g");
23       var b=("text"===d.type)?d.value:d.innerHTML;
24       return(b=="")?true:(e.exec(b))
25     }
26   });
27   $.fn.output=function(d){
28     if(typeof d=="undefined")
29       return (this.is(":text"))?this.val():this.html();
30     else
31       return (this.is(":text"))?this.val(d):this.html(d);
32   };
33   formatter={
34     getRegex:function(settings){
35       var settings=$.extend({type:"decimal",precision:5,decimal:'.',allow_negative:true},settings);
36       var result="";
37       if(settings.type=="decimal"){
38         var e=(settings.allow_negative)?"-?":"";
39         if(settings.precision>0)
40           result="^"+e+"\\d+$|^"+e+"\\d*"+settings.decimal+"\\d{1,"+settings.precision+"}$";
41         else result="^"+e+"\\d+$"
42       }else if(settings.type=="phone-number"){
43         result="^\\d[\\d\\-]*\\d$"
44       }else if(settings.type=="alphabet"){
45         result="^[A-Za-z]+$"
46       }
47       return result
48     },
49     isEmail:function(d){
50       var a=$(d).output();
51       var c=false;
52       var e=true;
53       var e=new RegExp("[\s\~\!\#\$\%\^\&\*\+\=\(\)\[\]\{\}\<\>\\\/\;\:\,\?\|]+");
54       if(a.match(e)!=null){
55         return c
56       }
57       if(a.match(/((\.\.)|(\.\-)|(\.\@)|(\-\.)|(\-\-)|(\-\@)|(\@\.)|(\@\-)|(\@\@))+/)!=null){
58         return c
59       }
60       if(a.indexOf("\'")!=-1){
61         return c
62       }
63       if(a.indexOf("\"")!=-1){
64         return c
65       }
66       if(email.tldn&&a.match(email.tldn)==null){
67         return c
68       }
69       return e
70     },
71     formatString:function(target,settings){
72       var settings=$.extend({type:"decimal",precision:5,decimal:'.',allow_negative:true},settings);
73       var oldText=$(target).output();
74       var newText=oldText;
75       if(settings.type=="decimal"){
76         if(newText!=""){
77           var g;
78           var h=(settings.allow_negative)?"\\-":"";
79           var i="\\"+settings.decimal;
80           g=new RegExp("[^\\d"+h+i+"]+","g");
81           newText=newText.replace(g,"");
82           var h=(settings.allow_negative)?"\\-?":"";
83           if(settings.precision>0)
84             g=new RegExp("^("+h+"\\d*"+i+"\\d{1,"+settings.precision+"}).*");
85           else g=new RegExp("^("+h+"\\d+).*");
86           newText=newText.replace(g,"$1")
87         }
88       }else if(settings.type=="phone-number"){
89         newText=newText.replace(/[^\-\d]+/g,"").replace(/^\-+/,"").replace(/\-+/,"-")
90       }else if(settings.type=="alphabet"){
91         newText=newText.replace(/[^A-Za-z]+/g,"")
92       }
93       if(newText!=oldText)
94         $(target).output(newText)
95     }
96   };
97   $.fn.format=function(settings,wrongFormatHandler){
98     var settings=$.extend({type:"decimal",precision:5,decimal:".",allow_negative:true,autofix:false},settings);
99     var decimal=settings.decimal;
100     wrongFormatHandler=typeof wrongFormatHandler=="function"?wrongFormatHandler:function(){};
101     this.keypress(function(d){
102       $(this).data("old-value",$(this).val());
103       var a=d.charCode?d.charCode:d.keyCode?d.keyCode:0;
104       if(a==13&&this.nodeName.toLowerCase()!="input"){return false}
105       if((d.ctrlKey&&(a==97||a==65||a==120||a==88||a==99||a==67||a==122||a==90||a==118||a==86||a==45))||(a==46&&d.which!=null&&d.which==0))
106         return true;
107       if(a<48||a>57){
108         if(settings.type=="decimal"){
109           if(settings.allow_negative&&a==45&&this.value.length==0)return true;
110           if(a==decimal.charCodeAt(0)){
111             if(settings.precision>0&&this.value.indexOf(decimal)==-1)return true;
112             else return false
113           }
114           if(a!=8&&a!=9&&a!=13&&a!=35&&a!=36&&a!=37&&a!=39){return false}
115           return true
116         }else if(settings.type=="email"){
117           if(a==8||a==9||a==13||(a>34&&a<38)||a==39||a==45||a==46||(a>64&&a<91)||(a>96&&a<123)||a==95){return true}
118           if(a==64&&this.value.indexOf("@")==-1)return true;
119           return false
120         }else if(settings.type=="phone-number"){
121           if(a==45&&this.value.length==0)return false;
122           if(a==8||a==9||a==13||(a>34&&a<38)||a==39||a==45){return true}
123           return false
124         }else if(settings.type=="alphabet"){
125           if(a==8||a==9||a==13||(a>34&&a<38)||a==39||(a>64&&a<91)||(a>96&&a<123))
126           return true
127         }else return false
128       }else{
129         if(settings.type=="alphabet"){
130           return false
131         }else return true
132       }
133     })
134     .blur(function(){
135       if(settings.type=="email"){
136         if(!formatter.isEmail(this)){
137           wrongFormatHandler.apply(this)
138         }
139       }else{
140         if(!$(this).is(":regex("+formatter.getRegex(settings)+")")){
141           wrongFormatHandler.apply(this)
142         }
143       }
144     })
145     .focus(function(){
146       $(this).select()
147     });
148     if(settings.autofix){
149       this.keyup(function(d){
150         if($(this).data("old-value")!=$(this).val())
151           formatter.formatString(this,settings)
152         }
153       )
154     }
155     return this
156   }
157 })(jQuery);