Created
April 21, 2010 18:36
-
-
Save metaskills/374214 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DecisivZipcodeInput = Class.create({ | |
initialize: function(input) { | |
this.input = input; | |
this.fivePattern = /^\d{5}$/; | |
this.fiveFourPattern = /^\d{5}-\d{4}$/; | |
this.maxLength = 10; | |
this.message = "Zipcode must be a 5-digit number with an optional 4-digit suffix seperated by a hyphen."; | |
this.input.writeAttribute('maxlength',this.maxlength); | |
this._initEvents(); | |
}, | |
enforceFormat: function() { | |
var value = this.input.getValue(); | |
var isBlank = value.blank(); | |
var matched = !isBlank && (value.match(this.fivePattern) || value.match(this.fiveFourPattern)); | |
if (isBlank || matched) { | |
return true; | |
} else { | |
alert(this.message); | |
this.input.select(); | |
}; | |
}, | |
_initEvents: function() { | |
this.input.observe('blur',this.enforceFormat.bindAsEventListener(this)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment