views:

50

answers:

2

Ok this is driving me crazy. I have a user account page. Where you have the option to change your password. I want a conditional jquery validation that if the user types in a new password in the new password box the box that confirms the password as well as the box that asks for the old password is turned into a required element. here is my ragtag code so far:

$("#aspnetForm").validate({
    rules: {
      <%=CurrentPass.UniqueID %>: {
          required: <%=NewPass1.UniqueID %>:specified}
      <%=NewPass2.UniqueID %>: {
          required: <%=NewPass1.UniqueID %>:specified}
    }, messages:{}
});

Just to clear something up. I am using :specified because if the filed is filled. Maybe some other condition?

A: 

Those selectors need to be strings using :filled (unless :specified is a custom selector you made) and found by ID instead of name, like this:

$("#aspnetForm").validate({
   rules: {
    <%=CurrentPass.UniqueID %>:{
     required: "#<%=NewPass1.ClientID %>:filled"}
    <%=NewPass2.UniqueID %>:{
     required: "#<%=NewPass1.ClientID %>:filled"}
    }, messages:{}    
   });
});
Nick Craver
Thanks man i hope this works haha this is my first shot at some jquery
Tom
Hmm this isnt working. The reason why i am using .uniqueID is because thats the way i was told to do it with my email. It will check and validate the email without a hitch. I cannot seem to get this to check and validate it is crazy.
Tom
@Tom - In this case you're using a selector though, think of it as `$(selector).length > 0`, that's literally the check it's performing.
Nick Craver
Do i really need to do the "#<%=NewPass1.ClientID %>:filled" I get that it is checking to see if the textfield literally has more that nothing in it. This is exactly what i want it to do. however this isnt falling into place for me. Also. what way can i check to see if NewPass2 = NewPass1
Tom
@Tom - You can, in that case your use `equalTo:"#<%=NewPass1.ClientID %>"` instead of `required`.
Nick Craver
A: 

I think you want to use equalTo for the confirmation password, with the current password required if the new password has data in it.

$('form').validate({
     rules: {
         <%= CurrentPass.UniqueID %>: {
                required: '#<%= NewPass1.ClientID %>:filled'
              },
         <%= NewPass2.UniqueID %>: {
                equalTo: '#<%= NewPass1.ClientID %>'
              }
     }
});
tvanfosson
I think I might have all my logic messed up. This just doesnt seem to work for me.
Tom
@Tom - first check that you don't have any javascript errors. second, check that the generated names in the rules match the names on the input elements, since the rules go by the names, not the ids. Presumably that's why you're using UniqueID (which I think is correct). I do *not* miss the whole name mangling thing in webforms.
tvanfosson
Yea this is driving me nuts. This is my first time doing javascript, even jquery and I think these are pretty decently hard concepts to grasph at first. I just want the current box and verify box required if the new pass box is typed in and compare the 2 new passes boxes.
Tom
@Tom - it's sufficient to require the current password and verify that the confirmation password is the same as the new password if a new password is supplied. If the new password is empty, then the confirmation should (must) be empty as well. If the new password isn't empty, then the old password is required and the confirmation must equal it.
tvanfosson
Yea i think thats what my logic essentially is saying. but apparently it isnt working haha. I will continue to mess with it. thank you! real quick question. why do i use UniqueID and then ClientID
Tom
I believe that the UniqueID is used as the name, where the ClientID is used as the id of the element. They should be the same modulo dollar and underscore characters. Honestly, you should probably double-check this to make sure I'm thinking right. Since I moved to MVC a year or so ago, I've swapped out a lot of my webforms knowledge.
tvanfosson
I know nothing on how to be able to figure it out. I assumed UniqueID would be what i would want to use for all. ill look into this. any suggestions as to where?
Tom
@Tom - the simplest thing is to look at the generated code and make sure that the rule names match the element names. That's all that matters.
tvanfosson