File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ ( ( ) => {
2
+ window . mm = ( i , mask ) => {
3
+ const d = { 9 : "[0-9]" , a : "[a-z]" } , msk = mask . split ( "" ) ;
4
+ i . addEventListener ( "input" , handler ) ;
5
+ i . addEventListener ( "focus" , handler ) ;
6
+
7
+ function handler ( e ) {
8
+ if ( e . type === "focus" && i . value !== "" ) return ;
9
+ let mskd = [ ] ,
10
+ s = i . selectionStart - 1 ;
11
+ msk . forEach ( ( el , n ) => {
12
+ if ( d [ el ] ) {
13
+ let t = new RegExp ( d [ el ] , "i" ) . test ( i . value . charAt ( n ) ) ;
14
+ mskd . push ( t ? i . value . charAt ( n ) : "_" ) ;
15
+ if ( t && s === n && i . value . charAt ( n ) !== "_" ) {
16
+ s ++ ;
17
+ }
18
+ } else {
19
+ mskd . push ( el ) ;
20
+ if ( s === n ) s ++ ;
21
+ }
22
+ } ) ;
23
+ i . value = mskd . join ( "" ) ;
24
+ i . selectionStart = i . selectionEnd = s < 0 ? 0 : s ;
25
+ setTimeout ( function ( ) {
26
+ i . selectionStart = i . selectionEnd = s < 0 ? 0 : s ;
27
+ } , 0 ) ;
28
+ }
29
+ } ;
30
+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments