I have a pretty good OOP JS structure in place, but am looking for some help with validating a username and email field. I have it pretty much working but I think the username and email could be validated better.
I have setup a delegate on a group of fields(elements) and then I perform validation on these. My code validation for username and email fields looks like:
} else if( regExpTest === "AjaxCall") {
if(currentValue.length > 0){
availability = ZEN.checkAvailability( $this ); //Check weather form input value is available in DB
availability.success(function (data) {
if(data == "1" && currentField == 'username'){
$('#usernameErrorExists').fadeIn();
}
if(data == "0" && currentField == 'username'){
$('#usernameErrorExists').fadeOut();
}
if(data == "1" && currentField == 'email'){
$('#emailErrorExists').fadeIn();
}
if(data == "0" && currentField == 'email'){
$('#emailErrorExists').fadeOut();
}
});
isValid = true;
}
}
The ZEN.checkAvailability function looks like:
ZEN.checkAvailability = function( input ) {
// get input name
var inputName = input.attr('name')
// get value
var inputValue = input.val();
return $.ajax({
type: "POST",
url: "/"+ inputName +"-check",
data: inputName +'='+ inputValue,
cache: false
});
};
My question is, is there a better way to validate the returned response from the AJAX request?
Thanks
Robert
Got this working better by altering the jQuery fades to a more appropriate time on the error messages.
Thanks to Chao for their comments.