I stumbled upon this code snippet online and I'm attempting to eliminate the Name must be letters only and min 3 and max 20 restriction. The name provided can have less than 3 characters and may include numbers. My JavaScript skills are still in the learning phase, so any assistance would be greatly appreciated.
(function($){
$.fn.checkAvailability = function(opts) {
opts = $.extend({
target: '#response',
trigger: '#btnCheck',
ajaxSource: 'test.asp',
fireOnKeypress: true
}, opts || {});
var $this = $(this);
if (opts.fireOnKeypress) {
$this.keyup(function() {
checkUsername();
});
$this.keypress(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
};
$(opts.trigger).click(function() {
checkUsername();
});
function checkUsername() {
if (validateUsername()) {
$(opts.target).html('<img src="loading.gif"> checking availability...');
usernameLookup();
}
else
{
$(opts.target).html('Name must be letters only and min 3 and max 20');
}
};
function usernameLookup() {
var val = $this.val();
$.ajax({
url: opts.ajaxSource,
data: {fn:val,s:Math.random()},
success: function(html){
$(opts.target).html(html);
},
error:function (){
$(opts.target).html('Sorry, but there was an error loading the document.');
}
});
};
function validateUsername(str) {
return (/^[A-Za-z]{3,20}$/.test($this.val()));
};
};
})(jQuery);