Skip to content

Instantly share code, notes, and snippets.

@tdegrunt
Created January 14, 2012 16:08
Show Gist options
  • Save tdegrunt/1611897 to your computer and use it in GitHub Desktop.
Save tdegrunt/1611897 to your computer and use it in GitHub Desktop.
RegExp Validator for batman.js (RegExpValidator)
class RegExpValidator extends Batman.Validator
@options 'with', 'message'
validateEach: (errors, record, key, callback) ->
value = record.get(key)
if !value? || !value.match(@options.with)
errors.add key, Batman.translate('errors.format', {attribute: key, message: @options.message})
callback()
Batman.Validators.push RegExpValidator
# Use with:
@validate 'email', with: /^[\w-]+(.[\w-]+)@([a-z0-9-]+(.[a-z0-9-]+)?.[a-z]{2,6}|(\d{1,3}.){3}\d{1,3})(:\d{4})?$/, message: "must be a valid email address"
@tdegrunt
Copy link
Author

Use with:
@Validate 'email', with: /^[\w-]+(.[\w-]+)@([a-z0-9-]+(.[a-z0-9-]+)?.[a-z]{2,6}|(\d{1,3}.){3}\d{1,3})(:\d{4})?$/, message: "must be a valid email address"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment