tags:

views:

26

answers:

2

I use this script daily and never had an issue before now. Ive been staring at it so long i can't find the issue. I have a form with an input, the input is required and has a minlength of 2. If you submit the form, it displays the "required" error message. If you enter one character and hit submit again, its adding another error message instead of changing between the two. Please help!This is using jquery.validate.js

<script type="text/javascript">
    $(document).ready(function() {
        $("#TTFirst").validate({
            errorElement: "span",
            errorPlacement: function(error, element) {
                error.appendTo( element.parent("td"));
            },

            rules: {
                    license: {
                    required: true,
                    minlength: 2
                }
            },

            messages: {
                license: {
                    required: "Please Enter Your First Name",
                    minlength: "Must be at Least 2 Characters"
                }

            }


        });

    });

</script>

HTML

<table cellspacing="1" id="credits">
    <form action="http://www.domain.com/dir/processor.php" method="post" id="TTFirst">
    <table>
    <tr class="odd">
    <td width="500">
        <label>$25 Transaction Credit for License </label>
            <input type="text" name="license" />
    </td>
    <td width="50">$26.95</td>
    <td width="150">
        <input type="hidden" name="item_number" value="41">
        <input type="submit" value="" class="orderNow" />
    </td>
        </tr>
    </table>
</form>
A: 

Because you are using the custom errorPlacement, it looks like the technique for messages is performed by adding the message to the title attribute of the label in question, and not in the javascript. Check this example code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
 <head>
 <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
 <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"&gt;&lt;/script&gt;
 <script>
  $(document).ready(function(){
   $("#myform").validate({
    errorPlacement: function(error, element) {
    error.appendTo( element.parent("td").next("td") );
   },
    debug:true
   })
  });
 </script>
 </head>

 <body>
  <form id="myform" action="/login" method="post">
   <table>
    <tr>
        <td><label>Firstname</label>
        <td><input name="fname" class="required" value="Pete" /></td>
        <td></td>
    </tr>
    <tr>
        <td><label>Lastname</label></td>
        <td><input name="lname" title="Your lastname, please!" class="required" /></td>
        <td></td>
    </tr>
    <tr>
        <td></td><td><input type="submit" value="Submit"/></td><td></td>
 </table>
 </form>
</body>
</html>

The error from the example only shows when the lname input is empty, and pulls from the title attribute.

Dave Kiss
@Dave Kiss - can you give me an example pls? Ive not run into this before and re using code as always
Dirty Bird Design
I take that back; I was using `errorLabelContainer` on my form which would make sense. It looks like using custom errors, you're supposed to define the Error Message in the title attribute of the form input, and not in the javascript messages. Give that a shot.
Dave Kiss
@Dave Kiss - ??? http://docs.jquery.com/Plugins/Validation/validate#toptions see the messages section
Dirty Bird Design
Right, but you are using the custom errorPlacement instead of errorLabelContainer - it looks like the two handle messages differently. Check my revised answer.
Dave Kiss
@Dave Kiss - still same issue. Im just going to put it in an UL. thanks for the help though!
Dirty Bird Design
+1  A: 

Damdest thing ive ever seen. It works fine when put in a

<ul>

something with the table I guess.

Dirty Bird Design
-1. Please use comments for making, well, comments.
SolutionYogi
@Solution Yogi - I used "answer my own question" because, I uhm, answered my own question. Thanks for policing this though, your time is much appreciated.
Dirty Bird Design
Undoing my vote. I see that you edited to include your fix. Your original answer did not make sense and I thought you were only ranting.
SolutionYogi