Merge branch 'JordanLittell-email-validation'

This commit is contained in:
Scott Erickson 2015-11-04 10:53:47 -08:00
commit 7752757888
3 changed files with 43 additions and 2 deletions

View file

@ -1,4 +1,11 @@
#subscription-view #subscription-view
.invalid-email-message
color: red
font-size: 12px
.recipient-emails.invalid
border: 1px solid red
.logged-out-blurb .logged-out-blurb
font-size: 18px font-size: 18px

View file

@ -150,7 +150,9 @@ block content
if view.recipientSubs.state === 'subscribing' if view.recipientSubs.state === 'subscribing'
.alert.alert-info(data-i18n="subscribe.subscribing") .alert.alert-info(data-i18n="subscribe.subscribing")
else else
textarea.recipient-emails(rows=3, data-i18n="[placeholder]subscribe.recipient_emails_placeholder") if emailValidator.state === 'invalid'
div.invalid-email-message(aria-hidden="true") please make sure all entries are valid emails
textarea.recipient-emails(rows=3, data-i18n="[placeholder]subscribe.recipient_emails_placeholder")=emailValidator.lastEmails
div div
button.recipients-subscribe-button.btn.btn-lg.btn-success(data-i18n="subscribe.subscribe_users") button.recipients-subscribe-button.btn.btn-lg.btn-success(data-i18n="subscribe.subscribe_users")
if view.recipientSubs.state === 'declined' if view.recipientSubs.state === 'declined'

View file

@ -49,9 +49,15 @@ module.exports = class SubscriptionView extends RootView
prepaidCode = utils.getQueryVariable '_ppc' prepaidCode = utils.getQueryVariable '_ppc'
@personalSub = new PersonalSub(@supermodel, prepaidCode) @personalSub = new PersonalSub(@supermodel, prepaidCode)
@recipientSubs = new RecipientSubs(@supermodel) @recipientSubs = new RecipientSubs(@supermodel)
@emailValidator = new EmailValidator(@superModel)
@personalSub.update => @render?() @personalSub.update => @render?()
@recipientSubs.update => @render?() @recipientSubs.update => @render?()
getRenderData: ->
c = super()
c.emailValidator = @emailValidator
c
# Personal Subscriptions # Personal Subscriptions
onClickStartSubscription: (e) -> onClickStartSubscription: (e) ->
@ -82,7 +88,8 @@ module.exports = class SubscriptionView extends RootView
onClickRecipientsSubscribe: (e) -> onClickRecipientsSubscribe: (e) ->
emails = @$el.find('.recipient-emails').val().split('\n') emails = @$el.find('.recipient-emails').val().split('\n')
@recipientSubs.startSubscribe(emails) valid = @emailValidator.validateEmails(emails, =>@render?())
@recipientSubs.startSubscribe(emails) if valid
onClickRecipientUnsubscribe: (e) -> onClickRecipientUnsubscribe: (e) ->
$(e.target).addClass('hide') $(e.target).addClass('hide')
@ -97,6 +104,31 @@ module.exports = class SubscriptionView extends RootView
# Helper classes for managing subscription actions and updating UI state # Helper classes for managing subscription actions and updating UI state
class EmailValidator
validateEmails: (emails, render) ->
@lastEmails = emails.join('\n')
#taken from http://www.regular-expressions.info/email.html
emailRegex = /[A-z0-9._%+-]+@[A-z0-9.-]+\.[A-z]{2,4}/
@validEmails = (email for email in emails when emailRegex.test(email.trim().toLowerCase()))
return @emailsInvalid(render) if @validEmails.length < emails.length
return @emailsValid(render)
emailString: ->
return unless @validEmails
return @validEmails.join('\n')
emailsInvalid: (render) ->
@state = "invalid"
render()
return false
emailsValid: (render) ->
@state = "valid"
render()
return true
class PersonalSub class PersonalSub
constructor: (@supermodel, @prepaidCode) -> constructor: (@supermodel, @prepaidCode) ->