views:

112

answers:

3

Yesterday I posted this question which is answered but it lead to another problem.

I have the following code using jquery validation plugin:

       //array of success messages and randomly select one 
   var messages = ["Good!", "Great!", "Awesome!", "Super!", "Nice!","Yay!", "Success!", "Ok!", "Perfect!","Sweet!" ];
function successMessage(label) {
return messages[Math.floor(Math.random() * messages.length)];
}

Then my validation code success

...success: function(label) {
                if(!$(label).hasClass("valid")){
          label.addClass("valid").text(successMessage());
            }
       }...

What is happening is that each time the form is being valid (on keyup or foucus) it regenerates a success message. I figured since i was adding the class "valid" a logical step would be to check if the label has the "valid" class and is so don't add a message because it already has one. This however isn't working. Any other ideas would be great.

Edit: So after further research and thinking. I'm pretty sure the problem is that the class of "valid" is being removed and added each time time form is validated (on keyup, submit, etc) I think the easiest thing to do may be to select the value of the label and look to see if the result matches the array, if so then don't do anything else add a success message. I could be wrong. I also don't think I articulated the question the best the first time. It just looks silly to have the validation message change while the user is typing.

EDIT 2 After the many suggestions for clarification and example code I am posting this link http://jsfiddle.net/Ye3Ls/20/ if you type into a field until you get a success message then keep typing you'll see what the problem is. I'm using the validation plugin found here Thanks for all your patients as I sort though how to get this to work. I think i may need to use something like in_array or has_value()

A: 

It looks like you mean to say:

$(label).addClass("valid").text(successMessage());

rather than:

label.addClass("valid").text(successMessage());
Matt S
I don't think this is the problem because label is a variable being defined by the validation plugin.
BandonRandon
A: 

If label is a variable, and label.addClass("valid") works fine, why don't you verify using:

if(!((label).hasClass("valid"))){

instead of if(!$(label).hasClass("valid")){

Liorsion
they do the exact same thing because label is the variable for $(label)
BandonRandon
+1  A: 

Don't need label as a parameter for successMessage.

function successMessage() {
    return messages[Math.floor(Math.random() * messages.length)];
}

I believe that if label is already a jQuery object doing this: $(label) will create a new label jQuery object attached to jQuery. Try this:

if(!label.hasClass("valid")){
    label.addClass("valid").text(successMessage());
}

Or

if(label.not(".valid")){
    label.addClass("valid").text(successMessage());
}

Or even better:

label.not(".valid").addClass("valid").text(successMessage());

[EDIT] after question asked in comment

if(label.text() === ''){
    label.not(".valid").addClass("valid").text(successMessage());
}
else {
    label.addClass("valid");
}

I'm assuming that you need to add the valid class to label. If you don't then remove the else statement.

I'm also assuming that the only way for text to be in label is through successMessage. Therefore, you will not need to add any text to label. It will stay the same.

On a side note, if the plug-in is changing the classes of your HTML elements then that is a serious side-effect of the code and I personally wouldn't put up with that.

Now the more logical thing that is probably happening is that you are reloading your content after doing your submission. If that is true then all the jQuery manipulations to the DOM you did before submission will be lost, because of new content be reloaded.

It would be really important in the future to add a link to the actual plug-in and more complete code to work with. @Nick Craver uses jsfiddle.net and I use jsbin.com to post sample code. This can be a more collaborative effort on all our parts to be able to reproduce and solve the problem.

[EDIT 1A] Here it is

The problem is that the label is being created more than once. This plug-in in my opinion is not so easy to work with.

I had to change the error placement and success logic.

    errorPlacement: function(label, element) {
    // position error label after generated textarea
    var name = element.attr('name');
    if ($('#' + name + '_label').length === 0) {

        label.attr('id', name + '_label');

        label.insertAfter(element);

        if (element.is("textarea")) {
            label.css({'padding-left': '105px'})
        }
    }
},

success: function(label) {
    var name = label.attr('for');
    $('#' + name + '_label').not('.valid').removeClass('error').addClass('valid').text(successMessage());
}
Gutzofter
Thanks, I think the would work but for some reason it's adding and removing the class everytime the form is validated. Is there a way to check if the value of label is in the `messages` array and if it is keep it the same?
BandonRandon
Thanks for your suggestions, I'm still learning how to write questions that make sense ;) I've updated the question again with an example showing the problem.
BandonRandon
If your not using firebug with FF when using jquery you should. Right away when I enter a name into `input #name` it generates a label with like this: `<label for="name" generated="true" class="error valid">Super!</label>`. So it looks like both error and valid classes are being applied to label.
Gutzofter
Great, I am using firebug and saw that it was adding a generated label just didn't really know what it ment. Also if you get a moment could you explain what your revised code is doing? it looks like you are creating a var to store the label name then adding the id so we have something like #name_label then check to see if the label name has the .valid class if not add the valid class. Ps, I agree this plugin isn't the easiest to work with, I normally just set the defaults and leave it.
BandonRandon
So this is just a little error but if you enter correct data get the success message. Then go back and enter incorrect data (say remove part of a valid e-mail address) the success message still shows. This isn't a huge deal but sorta strange.
BandonRandon
Yes, that is correct. The plug-in always creates an error label. So what I did is use that to only allow it to use one label, that is why I'm checking to see if the label already exists with the `.length === 0` check. The plug-in instead of generating a new label every time should generate a label from the get go then `display: block;` on in error with `.error` or on success `display: block;` with `.valid`
Gutzofter