Skip to content

Instantly share code, notes, and snippets.

@wraithgar
Created August 15, 2016 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wraithgar/6431eb458c981fe6c0a8a7ee71754945 to your computer and use it in GitHub Desktop.
Save wraithgar/6431eb458c981fe6c0a8a7ee71754945 to your computer and use it in GitHub Desktop.
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