views:

56

answers:

4

i'm trying to validate an html page using jquery, but nothing happens. it's a simple page, yet the textboxes aren't validated.

this is the code:

<link rel="stylesheet" type="text/css" href="style.css" /> 
<script type="text/javascript" src="~/jquery.js/"></script>
<script type="text/javascript" src="~/jquery-validate/jquery.validate.js/"></script>
<script type="text/javascript">
  $(document).ready(function(){
    var validator = $("#submitForm").validate({
            debug: true,
            rules: {
                author: "required",
                quote: "required"
            },
            messages: {
                author: "Name of Author required"
                quote: "Please enter Quote"
            },
            errorPlacement: function(error, element) {
                error.appendTo( element.parent().top() );
            }
      );
  });
  </script>

<body>

<div class="container">
<div class="center_div">
<h2>Submit Your Quote</h2>
<fieldset>
<form id="submitForm" action="qinsert.php" method="post">
<div class="error" style="display:none"></div>
<div class="field">
<label>Author: </label>
<input name="author" type="text" class="required" minLength=3>
<span class="error"></span>
</div><br />
<div class="field">
<label>Quote: </label>
<textarea name="quote" cols=22 class="required" minLength=5></textarea>
<span class="error"></span>
<br />
</div>
<input id="button1" type="submit" value="Submit" class="submit" /><br />
<input id="button2" type="reset" value="Reset" /> 
</form>
</fieldset>
</div>
</div>

what is wrong with the code? thank you!

+1  A: 

Just a quick glance, it looks like you are missing a closing '}' in your javascript. I think you are not closing the validate({ correctly. Try to add the } brace in front of your ); after the errorPlacement function.

  $(document).ready(function(){ 
var validator = $("#submitForm").validate({ 
        debug: true, 
        rules: { 
            author: "required", 
            quote: "required" 
        }, 
        messages: { 
            author: "Name of Author required" 
            quote: "Please enter Quote" 
        }, 
        errorPlacement: function(error, element) { 
            error.appendTo( element.parent().top() ); 
        } 
  }); 
  }); 
Tommy
See comment on your answer post for further explanation on the script tag src attributes
Tommy
+1  A: 

I haven't used jquery validation plugin before, but there is obviously a missing comma here:

messages: 
{
            author: "Name of Author required",
            quote: "Please enter Quote"
},

Don't have this plugin by hand so I cannot get a sample running code now.

Personally I feel a declarative way of validation is more appealing to me, which means rather than put the validation requirements directly into your javascript code, you can declaratively mark them inside html. The validation engine then will go check each required field, etc...

If this sounds reasonable to you, then I recommend another validation plugin : jQuery inline validation

Michael Mao
A: 

did that, still doesn't validate. firebug throws this error:

 $ is not defined
 $(document).ready(function(){ 

what does this mean?

fuz3d
Ahhh, this means you failed to load jQuery... Try changing it to the latest compressed version and see if that will do. Just another wondering, what if you remove the leading "~" in your script path? Try using something relative as "./"
Michael Mao
In the future, use the "add comment" button below answers to reply to them rather than opening a new answer. Answers are not sorted chronologically so the ordering is not preserved.
musicfreak
It means Jquery is not loaded. Your script links don't look right either. The '~' is an .NET thing - it looks like you are using PHP. Try hard coding your script file resources like this: <script type="text/javascript" src="../scripts/jquery.js/"></script> or whatever your path is.
Tommy
@Tommy : oh, that strange look comes from .NET... I was wondering if that refers to the "user's home directory" as in Linux :)
Michael Mao
thank you, Michael and Tommy. it worked! the issue was with the name of the js file which was 'jquery-1.4.2' instead of plain jquery.Michael, i've used jquery in the past on the .NET platform - that explains ~. i'm learning PHP for a change. ;)
fuz3d
@MichaelMao - Yup, one of thoes .NET things, but funny you mention it - thats what the ~/, it maps back to your application root...didn't know that Linux has something similar! :)
Tommy
A: 

Check the path to your jQuery libraries. Them being in a directory named ~ and suffixed with / seems unlikely. If they are in the same directory the correct syntax is:

<script type="text/javascript" src="jquery.js"></script>
wombleton