views:

32

answers:

1

I got 2 forms in 2 different div's which are hidden, if the user clicks on one (login, register) the relevant div is shown.

I've named the form fields like reg_username, reg_email ... and the login fields are named login_username login_password.

Now i've created a function which validate the fields. When i step through with this part of code

$("#account").find(':input').each(function(i) {
});

it finds every form field.

the div #account is my main div where all fields are in from the 2 div #register and #login. There are serval other input fields in those 2 div's.

Depending on the div i only want to check fields starting with a tag (reg or login), is there a way to get only this div's and not all?

+1  A: 

For register:

$(":input[name^='reg_']").each(function(){
 // validation stuff here
});

and for login:

$(":input[name^='login_']").each(function(){
 // validation stuff here
});

These selectors match only those form inputs whose name attribute starts with reg_ and login_, respectively.

If your 'tags' (reg_ and login_) are in the id of the elements and not the name, just change name to id in the code above.

See the attributeStartsWith jQuery selector.

Josh Leitzel
Update my question a little bit, there a serval other form fields in thos div's. The div account is my main div (where the 2 div's #register and #login are in)
I'm still a little confused, but I think I understand what you want. Updated my answer.
Josh Leitzel
That worked out of the box, thanks a lot! Could you tell me why you where confused? So that the next question i might ask is more clear.