Skip to content

Instantly share code, notes, and snippets.

@metaskills
Created April 21, 2010 18:36
Show Gist options
  • Save metaskills/374214 to your computer and use it in GitHub Desktop.
Save metaskills/374214 to your computer and use it in GitHub Desktop.
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