rockinaway Posted August 10, 2011 Share Posted August 10, 2011 Right, I have the following set of code to check form entires: $('#username').keyup(function () { var t = this; if (this.value != this.lastValue) { if (this.timer) clearTimeout(this.timer); validateUsername.html('<img src="templates/img/loader.gif" height="16" width="16" /> checking availability...'); this.timer = setTimeout(function () { $.ajax({ url: 'register.php', data: 'action=check_username&username=' + t.value, type: 'post', dataType: 'json', success: function (j) { if (j.ok === true) { $("#register").removeAttr("disabled"); field_success(validateUsername); validateUsername.html(j.msg); } else { $("#register").attr("disabled", "disabled"); field_error(validateUsername); validateUsername.html(j.msg); } } }); }, 200); this.lastValue = this.value; } }); This works perfectly fine when I'm trying to input data etc. However, I am trying to convert this into a separate function that can be called again. So something like this: function check_user(this) { var t = this; if (this.value != this.lastValue) { if (this.timer) clearTimeout(this.timer); validateUsername.html('<img src="templates/img/loader.gif" height="16" width="16" /> checking availability...'); this.timer = setTimeout(function () { $.ajax({ url: 'register.php', data: 'action=check_username&username=' + t.value, type: 'post', dataType: 'json', success: function (j) { if (j.ok === true) { $("#register").removeAttr("disabled"); field_success(validateUsername); validateUsername.html(j.msg); } else { $("#register").attr("disabled", "disabled"); field_error(validateUsername); validateUsername.html(j.msg); } } }); }, 200); this.lastValue = this.value; } } $('#username').keyup(function () { check_user(this); }); However, this doesn't work AT ALL. What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/244451-converting-to-a-function/ Share on other sites More sharing options...
nogray Posted August 10, 2011 Share Posted August 10, 2011 Change function check_user(this) to function check_user() (remove this) change $('#username').keyup(function () { check_user(this);}); to $('#username').keyup(check_user); Link to comment https://forums.phpfreaks.com/topic/244451-converting-to-a-function/#findComment-1255608 Share on other sites More sharing options...
rockinaway Posted August 11, 2011 Author Share Posted August 11, 2011 Still not working .. I have the following: function check_user() { var t = this; if (this.value != this.lastValue) { if (this.timer) clearTimeout(this.timer); validateUsername.html('<img src="templates/img/loader.gif" height="16" width="16" /> checking availability...'); this.timer = setTimeout(function () { $.ajax({ url: 'register.php', data: 'action=check_username&username=' + t.value, type: 'post', dataType: 'json', success: function (j) { if (j.ok === true) { $("#register").removeAttr("disabled"); field_success(validateUsername); validateUsername.html(j.msg); } else { $("#register").attr("disabled", "disabled"); field_error(validateUsername); validateUsername.html(j.msg); } } }); }, 200); this.lastValue = this.value; } } $(document).ready(function () { var validateUsername = $('#user_error'); $("#register").attr("disabled", "disabled"); $('#username').keyup(check_user); }); Link to comment https://forums.phpfreaks.com/topic/244451-converting-to-a-function/#findComment-1255726 Share on other sites More sharing options...
rockinaway Posted August 12, 2011 Author Share Posted August 12, 2011 anyone? Link to comment https://forums.phpfreaks.com/topic/244451-converting-to-a-function/#findComment-1256353 Share on other sites More sharing options...
Omirion Posted August 17, 2011 Share Posted August 17, 2011 If you want to scrap the entire idea you can use this jQuery plugin: http://docs.jquery.com/Plugins/validation Hassle free. Alternatively, are there any console errors with the first code version. Link to comment https://forums.phpfreaks.com/topic/244451-converting-to-a-function/#findComment-1258803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.