I have a form that calls a function using the onClick event in the submit button. I would like this function to return false if the username is taken. The the original function, which I've tested, returns the correct info when the "checkUname " button is clicked. However, it always returns false. I want to return false ONLY if the username is taken.
Original:
I would like to run the same code, from the submit button and return true if the value of "resp" is false. This is what I've done:
Field I wish to validate:
Submit button:
checkuname function:
So, if the checkuname function returns false then the form won't submit and it also shows this div that has the class of "toggleFailure" . Right now it's only returning false if I leave the field blank. As soon as I put any values in there, the form submits regardless of the checkuname function.
I should note (in case it might be relevant) that before this function in the JS file, I have a call to the JQuery validate plugin which I call like so:
Also, the name and ID of the form are: "newUserFor m".
Original:
Code:
$("#checkUname").click(function(){
var datastring= $('form#newUserForm').serialize();
$.ajax({
type: "POST",
url: "index.cfm?event=user.checkUname&requestFormat=ajax",
data: datastring,
success: function(response){
var resp = jQuery.trim(response);
if (resp == 'FALSE'){
$('.toggleSuccess').show();
$('.toggleFailure').hide();
}else{
$('.toggleFailure').show();
$('.toggleSuccess').hide();
}
}
});// .ajax()
return false;
}); // .submit()
Field I wish to validate:
Code:
<input class="text required" name="uname" type="text" />
Code:
<input name="submit" type="submit" onclick="checkuname()" />
Code:
function checkuname(){
var datastring = document.getElementById('uname');
$.ajax({
type: "POST",
url: "index.cfm?event=user.checkUname&requestFormat=ajax",
data: datastring,
success: function(response){
var resp = jQuery.trim(response);
if (resp == 'FALSE'){
return true;
}else{
$('.toggleFailure').show();
$('.toggleSuccess').hide();
return false;
}
}
});// .ajax()
};
I should note (in case it might be relevant) that before this function in the JS file, I have a call to the JQuery validate plugin which I call like so:
Code:
$('form#newUserForm').validate();
Comment