Created
August 15, 2016 21:46
-
-
Save wraithgar/6431eb458c981fe6c0a8a7ee71754945 to your computer and use it in GitHub Desktop.
async foundation validation example
This file contains hidden or 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
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