Created
September 22, 2015 08:13
-
-
Save cAstraea/0eb44cd4d8310a325220 to your computer and use it in GitHub Desktop.
validate fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
extendsFrom: 'CreateActionsView', | |
initialize: function (options) { | |
this._super('initialize', [options]); | |
//add validation tasks | |
app.error.errorName2Keys['field_error'] = 'Invalid Email'; | |
this.model.addValidationTask('check_email', _.bind(this._doValidateEmail, this)); | |
}, | |
_doValidateEmail: function(fields, errors, callback) { | |
//validate against the API | |
var primaryEmail; var emailArray = this.model.get('email'); | |
if (!_.isEmpty(emailArray)) { | |
console.log(emailArray); | |
_.each(emailArray, function(email) { | |
primaryEmail = email.email_address; | |
$.ajax({ | |
url: 'https://pozzad-email-validator.p.mashape.com/emailvalidator/validateEmail/' + primaryEmail, | |
headers:{"X-Mashape-Key": "sHJJhFj2YbmshviJLeC8JYv7W8jCp1RhasqjsnwtBFZPu2uDPR", | |
"Content-Type": "application/json" | |
}, | |
success: function(data) { | |
console.log(data.isValid); | |
if(!data.isValid) | |
{ | |
alert('invalid email'+primaryEmail); | |
errors['email'] = errors['email'] || {}; | |
errors['email'].field_error = true; | |
callback(null, fields, errors); | |
} | |
else { | |
// alert('valid email'); | |
//email is valid , do callback | |
callback(null, fields, errors); | |
} | |
} | |
}); | |
}, this); | |
} | |
else { | |
// alert('empty email field'); | |
errors['email'] = errors['email'] || {}; | |
errors['email'].required = true; | |
callback(null, fields, errors); | |
} | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment