Skip to content

Instantly share code, notes, and snippets.

@codeprimate
Created May 19, 2009 16:11
Show Gist options
  • Save codeprimate/114192 to your computer and use it in GitHub Desktop.
Save codeprimate/114192 to your computer and use it in GitHub Desktop.
// Requires Prototype.js
ConfirmedEmailValidator = Class.create(
{
initialize: function(email_input, confirmation_input) {
this.valid_class = 'valid'
this.invalid_class = 'invalid'
this.email_id = email_input
this.confirmation_id = confirmation_input
this.re = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
this.add_observers()
},
add_observers: function() {
document.observe("dom:loaded", function() {
this.email = $(this.email_id)
this.confirmation = $(this.confirmation_id)
this.email.observe('change', this.display_validity.bind(this))
this.confirmation.observe('keyup', this.display_validity.bind(this))
}.bindAsEventListener(this))
},
valid_email: function(){
return (this.email.value.match(this.re) != null)
},
valid_confirmation: function() {
return (this.confirmation.value.match(this.re) != null)
},
matching: function() {
return (this.email.value == this.confirmation.value)
},
valid: function() {
return (this.matching() && this.valid_email() && this.valid_confirmation() )
},
display_validity: function(){
if (this.valid()) {
this.email.removeClassName(this.invalid_class)
this.email.addClassName(this.valid_class)
this.confirmation.removeClassName(this.invalid_class)
this.confirmation.addClassName(this.valid_class)
} else {
if (this.valid_email()) {
this.email.removeClassName(this.invalid_class)
this.email.addClassName(this.valid_class)
} else {
this.email.removeClassName(this.valid_class)
this.email.addClassName(this.invalid_class)
}
if (this.valid_confirmation){
this.confirmation.removeClassName(this.invalid_class)
this.confirmation.addClassName(this.valid_class)
} else {
this.confirmation.removeClassName(this.valid_class)
this.confirmation.addClassName(this.invalid_class)
}
if (!this.matching() ) {
this.confirmation.removeClassName(this.valid_class)
this.confirmation.addClassName(this.invalid_class)
}
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment