Monday, August 31, 2009

[jQuery] Validation question

I have this script
 
$(document).ready( function() {
 
    $('#username').blur( function () {
 
        fieldName = $(this).attr('id');
        fieldValue = $(this).val();
 
        $.post('/users/ajax_validate', {
                                        field: fieldName,
                                        value: fieldValue
                                        },
               function(error) {
 
                   if(error.length != 0) {               
 
                       $('#username').after('<div class="error-message" id="'+ fieldName +'-exists">' + error + '</div>');
                   }
                   else {
                       $('#' + fieldName + '-exists').remove();
                   }
               });
     });  
 
});
 
Which sends a requet to determine if the username is taken...pretty simple if so it displays Please choose another message. My problem is if the user goes back to the field and then leaves without changing it it sends the request again and then shows
"Please choose another name"
"Please choose another name"
 
How can I modify the script so if there is an error and not fixed to leave the message and not repeat it?
 
Dave

No comments: