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
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
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")};
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))
27 $.fn.output=function(d){
28 if(typeof d=="undefined")
29 return (this.is(":text"))?this.val():this.html();
31 return (this.is(":text"))?this.val(d):this.html(d);
34 getRegex:function(settings){
35 var settings=$.extend({type:"decimal",precision:5,decimal:'.',allow_negative:true},settings);
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"){
53 var e=new RegExp("[\s\~\!\#\$\%\^\&\*\+\=\(\)\[\]\{\}\<\>\\\/\;\:\,\?\|]+");
57 if(a.match(/((\.\.)|(\.\-)|(\.\@)|(\-\.)|(\-\-)|(\-\@)|(\@\.)|(\@\-)|(\@\@))+/)!=null){
60 if(a.indexOf("\'")!=-1){
63 if(a.indexOf("\"")!=-1){
66 if(email.tldn&&a.match(email.tldn)==null){
71 formatString:function(target,settings){
72 var settings=$.extend({type:"decimal",precision:5,decimal:'.',allow_negative:true},settings);
73 var oldText=$(target).output();
75 if(settings.type=="decimal"){
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")
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,"")
94 $(target).output(newText)
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))
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;
114 if(a!=8&&a!=9&&a!=13&&a!=35&&a!=36&&a!=37&&a!=39){return false}
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;
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}
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))
129 if(settings.type=="alphabet"){
135 if(settings.type=="email"){
136 if(!formatter.isEmail(this)){
137 wrongFormatHandler.apply(this)
140 if(!$(this).is(":regex("+formatter.getRegex(settings)+")")){
141 wrongFormatHandler.apply(this)
148 if(settings.autofix){
149 this.keyup(function(d){
150 if($(this).data("old-value")!=$(this).val())
151 formatter.formatString(this,settings)