98143bd59423d34790c9f42f886c1f86

procedure check user information and add addition for full pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function checkInterval(event) {
	if ( true == new RegExp( /^\d$/ ).match( event.element().value ) ) {
		event.element().value = '00:0' + event.element().value;
	}
	if ( true == new RegExp( /^\d\d$/ ).match( event.element().value ) ) {
		event.element().value = '00:' + event.element().value;
	}
	if ( true == new RegExp( /^:\d\d$/ ).match( event.element().value ) ) {
		event.element().value = '00' + event.element().value;
	}
	if ( true == new RegExp( /^\d:\d\d$/ ).match( event.element().value ) ) {
		event.element().value = '0' + event.element().value;
	}
	if ( false == new RegExp( /^\d\d:\d\d$/ ).match( event.element().value )) {
		alert( event.element().value + ' является неправильным значением' );
		event.element().value = event.element().value.substr(0,5);
	}
	if ( -1 == event.element().value.indexOf( ':' ) ) {
		event.element().value = '';
	}
}

Refactorings

No refactoring yet !

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

September 14, 2009, September 14, 2009 16:50, permalink

No rating. Login to rate!

Not sure i understand what this is supposed to do...

A8d3f35baafdaea851914b17dae9e1fc

Adam

September 14, 2009, September 14, 2009 21:20, permalink

1 rating. Login to rate!
1
2
3
4
5
6
7
8
9
function checkInterval(event) {
    var pad = function(s) { return Array(3 - s.length).join('0') + s }
    
    if (matches = event.element().value.match(/^((\d{1,2}):)?(\d{1,2})$)/) {
        event.element().value = pad(matches[1] || '') + ':' + pad(matches[2] || '');
    } else {
        event.element().value = '';
    }
}
0746bcb6c9657f8bf18aa0aa8809d27c

peernohell.myopenid.com

December 11, 2009, December 11, 2009 13:06, permalink

No rating. Login to rate!

@insanegigolo.livejournal.com : in your function you can't use match on RegExp, instead use test.
@Adam : you don't take good matches. it's matches[2] and matches[3]
for better perf use constant for pad and regexp. like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var Interval = (function () {
  var pad = function(s) { return s.length < 2 ? pad('0'+s) : s; }
  var regexp = /^((\d{1,2}):)?(\d{1,2})$/;

  return {
    check: function (event) {
      if (matches = event.element().value.match(regexp) ){
        event.element().value = pad(matches[2] || '') + ':' + pad(matches[3] || '');
      } else {
        event.element().value = '';
      }
    }
  }
})();
2d64c3f131ddd235c8f44224e83be221

dkeExG

December 22, 2009, December 22, 2009 15:13, permalink

No rating. Login to rate!

Hi! xghEmI

1
Hi! xghEmI

Your refactoring





Format Copy from initial code

or Cancel