Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
async foundation validation example
var checkingLogin = false;
var validLogin = true;
var lastCheckedLogin = '';
var checkLogin = Debounce(function (el) {
var code = document.location.search.match(/invite=([^&]*)/);
var val = el.val();
if (code) {
code = code[1];
}
if (checkingLogin || lastCheckedLogin === el.val()) {
return;
}
if (val === '') {
//We're being called on the debounce tail after a bunch of backspaces
return;
}
checkingLogin = true;
lastCheckedLogin = val;
var payload = {
login: val,
invite: code
};
var syncOptions = {
url: App.apiUrl + '/taken',
json: payload,
success: function (resp) {
checkingLogin = false;
validLogin = !resp.data.taken;
el.trigger('change.fndtn.abide');
},
error: function () {
checkingLogin = false;
validLogin = false;
el.trigger('change.fndtn.abide');
}
};
Sync('create', null, syncOptions);
}, 200);
//In app.init:
$(document).foundation({
abide: {
validators: {
checkLogin: function (el, required) {
el = $(el);
if (required && el.val() === '') {
el.siblings('small').text('Login is required');
return false;
}
el.siblings('small').text('Login is taken');
checkLogin(el);
return validLogin;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment