Skip to content

Instantly share code, notes, and snippets.

@pketh
Last active August 29, 2015 14:18
Show Gist options
  • Save pketh/84ee7ac16091b5c58b51 to your computer and use it in GitHub Desktop.
Save pketh/84ee7ac16091b5c58b51 to your computer and use it in GitHub Desktop.
validates emails with hooks for doing other things
validateEmail = (email, input) ->
emailPattern = /^[A-Za-z0-9](([_\.\-+]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/i
if emailPattern.test(email)
addSuccessCookie()
return true
else
input.addClass('fail')
return false
addSuccessCookie = () ->
console.log "on success, I add the success cookie"
$('.submit-link').click ->
form = $(this).prev()
form.trigger('submit')
$('.newsletter-form').submit ->
input = $(this).find('input')
email = input.val().trim()
validateEmail(email, input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment