Check the error message existence before adding a new error msg

if ($('#'+fieldName+'-exists').length = 0)
   $('#username').after('<div class="error-message"id="'+ fieldName +'-
exists">' + error + '</div>');

On Aug 12, 2:17 am, "Dave Maharaj :: WidePixels.com"
<d...@widepixels.com> wrote:
> 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();
>                    }
>                });
>      });  
>
> });
>
> So when the user leaves the field it checks validation. If error it shows an
> error message. Everything is working fine except if the user goes back to
> the field where there was an error and does not chnge anything and leaves
> the field again the error message is duplicated.
>
> So they enter a username peter it says "Please choose a different name" if
> they go back to the username and thenleavewith out changing "peter" i now
> have:
>  "Please choose a different name"
> "Please choose a different name"
>
> What do I have to change / add so that only 1 message will appear?
>
> Thanks
>
> Dave

Reply via email to