Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Issue with equalTo in validator

    I am having an issue with my jquery validator. I am trying to get 2 password fields to equal each other but it doesn't seem to work.

    HTML:
    PHP Code:
    <form id="register-form">
        <
    div class="form-group" data-group="login-register" data-item="register-password">
            <
    div class="col-xs-12">
                <
    div class="input-group">
                    <
    input type="password" name="password" value="" class="form-control" maxlength="128" placeholder="Password"  /><span class="input-group-addon"><class="fa fa-asterisk fa-fw"></i></span>
                </
    div>
            </
    div>
        </
    div>
        <
    div class="form-group" data-group="login-register" data-item="register-password2">
            <
    div class="col-xs-12">
                <
    div class="input-group">
                    <
    input type="password" name="password2" value="" class="form-control" maxlength="128" placeholder="Password"  /><span class="input-group-addon"><class="fa fa-asterisk fa-fw"></i></span>
                </
    div>
            </
    div>
        </
    div>
    </
    form
    jQuery:
    Code:
    rules:{
        password:{
            required: true,
            minlength: 8,
            maxlength: 128,
        },
        password2:{
            required: true,
            minlength: 8,
            maxlength: 128,
            equalTo: "#register-form > input[name=password]",
        },
    },
    I cut out anything else that isn't relevant to the request just to keep the code smaller, I know my form doesn't have an action or anything else, I removed that as well (even though I'm using jQuery so I don't need it).

    The main focus is to the equalTo for the password rules, it keeps failing and I don't see why. I've tried a few combinations but I'm drawing a blank. Any help would be appreciated! I know it's probably something simple. I know I could add an ID to the input and call it directly, but I'd prefer to tier to it based on the forum in case I ever use the same field name in different forums.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    since whitespace around ">" in css is eaten by the parser, "#register-form > input[name=password]" doesn't point to your input, only to inputs whose parent tag is a from element with the id "register-form", which there are none.
    just "#register-form input[name=password" will hit both password fields, but one will always match itself right, so that should be ok... i believe you can also use $("assword", myForm) to hit the input without have and id on the form.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Thanks for the reply!

    I believe before I climbed down the latter from parent to child, specifying each element type as it went and it seemed to work, however I don't want to have to change something if the template changes and I have to add/remove some elements. I want to be able to have the input in the form, but not matter how many elements are in between.

    I took the arrow out of the string as you suggested but left the ending bracket for the name and it seems to be fine now, thanks!
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •