natalie
natalie

Reputation: 3

validating a field using jquery

rather than using a jquery plugin. if i have two fields and the first field is required and is currently on focus. if i click or tab over to the next field how can i check with jquery to see if the field contains any data and if it doesnt append some text or a class next to it? is onblur() used or?

thanks

Upvotes: 0

Views: 79

Answers (3)

dafunkeemonkee
dafunkeemonkee

Reputation: 4105

Yes. Handling the blur event should work just fine. Rough implementation below. You may need to check for whitespaces just in case.

$('input[name=firstfield]').blur(function() {
   if ($(this).val().length == 0) { 
      // Add class, text, etc.
   }
});

Upvotes: 1

Rafay
Rafay

Reputation: 31033

if i have not misunderstood you can use .focus() and .focusout()

here is an example

Upvotes: 0

TNC
TNC

Reputation: 5386

Yeah, you're close.

You could do something like:

$('input[name="data"]').blur(function()
{
    if($(this).val().length == 0)
    {
        // add text or whatever
    }
});

Upvotes: 0

Related Questions