0

I want to use validation messages on a form, but when I click on the botton nothing happens and I don't know why. I have struggled with this problem for a while now and nobody could have help me really. I am desperate at this point to get this working... Here is my code:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Module Health Measurements</title>

          <!-- Load jQuery and the validate plugin -->
         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">

        <script>
            $.validator.setDefaults({submitHandler: function() { alert("submitted!");}});
            $(document).ready(function() {
               $("#form1").validate({
                    rules: {
                        40: {required : true, minlength : 2, maxlength : 3 },
                        41: {required : true, minlength : 2, maxlength : 3 },
                        42: {required : true, minlength : 2, maxlength : 3 },
                        43: {required : true, minlength : 2, maxlength : 3 },
                        44: {required : true, minlength : 2, maxlength : 3 },
                        45: {required : true },
                        46: {required : true },
                        47: {required : true },
                        48: {required : true },
                        51: {required : true },
                        52: {required : true },
                    } ,
                    messages: {
                        40: {required : 'Your height in cm without shoes is required', minlength : 'No less than 2 characters',
                        maxlength : 'No more than 3 characters' },
                        41: {required : 'Your weight in kg without shoes is required', minlength : 'No less than 2 characters',
                        maxlength : 'No more than 3 characters' },
                        42: {required : 'Your hip circumference in cm is required', minlength : 'No less than 2 characters',
                        maxlength : 'No more than 3 characters' },
                        43:{required : 'Your waist circumference in cm is required', minlength :' No less than 2 characters',
                        maxlength : 'No more than 3 characters' },
                        44: {required : 'A selection is required' },
                        45: {required : 'Systolic blood pressure mmHg is required' },
                        46: {required : 'Diastolic blood pressure mmHg is required' },
                        47: {required : 'Glucose mmol/l is required' },
                        48: {required : 'Total Cholesterol mmol/l is required' },
                        51: {required : 'Systolic blood pressure 5min is required' },
                        52: {required :' Diastolic blood pressure 5min is required' },
                    }
                });
            });
        </script>

        <style type="text/css">
        #form1{ width:400px;}
        </style>

        </head>
        <body>

        <form id="form1" name="form1" method="post" action="">

        <table width="800" border="1">
          <tr>
            <td colspan="2" bgcolor="#5ACDC7">Health Measurements</td>
          </tr>
          <tr>
            <td width="636">Height in cm without shoes</td>
            <td width="148"><label>
              <input name="40" type="text" id="40" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Weight in kg without shoes</td>
            <td><label>
              <input name="41" type="text" id="41" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Hip circumference in cm</td>
            <td><label>
              <input name="42" type="text" id="42" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Waist circumference in cm</td>
            <td><label>
              <input name="43" type="text" id="43" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Have you eaten in the last 7 hours?</td>
            <td><label>
              <select name="44" id="44">
                <option>Yes</option>
                <option>No</option>
              </select>
            </label></td>
          </tr>
          <tr>
            <td>Systolic blood pressure mmHg</td>
            <td><label>
              <input name="45" type="text" id="45" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Diastolic blood pressure mmHg</td>
            <td><label>
              <input name="46" type="text" id="46" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Glucose mmol/l</td>
            <td><label>
              <input name="47" type="text" id="47" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Total Cholesterol mmol/l</td>
            <td><label>
              <input name="48" type="text" id="48" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" bgcolor="#5ACDC7">BP 5 Minute Follow-up</td>
          </tr>
          <tr>
            <td>Systolic blood pressure 5 min</td>
            <td><label>
              <input name="51" type="text" id="51" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>Diastolic blood pressure 5 min</td>
            <td><label>
              <input name="52" type="text" id="52" size="7" maxlength="7" />
            </label></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><label>
              <input type="submit" name="button1" id="button1" value="Submit" />
            </label></td>
            <td>&nbsp;</td>
          </tr>
        </table>

        </form>

        </body>
        </html>

Can anyone please send me a link where I can download a proper .css stylesheet... Any help would really be appreciated. Thanx

Sparky
  • 98,165
  • 25
  • 199
  • 285
Wimpie Brits
  • 11
  • 1
  • 3
  • 2
    Are you sure your problem is with the stylesheet? I don't really understand your question. – Sébastien Oct 21 '13 at 14:56
  • Have you checked the console for errors? I suspect the issue is you have not included the jquery validate plugin js reference. – mayabelle Oct 21 '13 at 15:26
  • CSS has **nothing** to do with jQuery Validate to operating correctly. – Sparky Oct 21 '13 at 16:28
  • Duplicate of [jquery validation - messages not displaying correctly](http://stackoverflow.com/questions/19491647/jquery-validation-messages-not-displaying-correctly) ~ Please do not post duplicates. – Sparky Oct 21 '13 at 16:41
  • Your `name` and `id` values cannot be `40`, `41`, etc.; it's not valid as per your `doctype`. – Sparky Oct 21 '13 at 17:04

2 Answers2

1

Your code is valid, but you forget to add the validation plugin, you added the css file, but no the js file, you can downlad it from http://jqueryvalidation.org/, and just add the line:

<script src="js/jquery.validate.min.js"></script>

I tried it, and its working

user2891084
  • 111
  • 4
  • @WimpieBrits, the file also needs to be uploaded to your server at the location specified by the `src` value. – Sparky Oct 21 '13 at 17:06
0

You forgot to include the plugin in your code. I see jQuery, but the jQuery Validate plugin is missing...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Module Health Measurements</title>

        <!-- Load jQuery and the validate plugin -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">

However, your doctype does not allow you to use numbers to start your name and id values. It's not valid HTML unless you're using HTML5. See: https://stackoverflow.com/a/79022/594235

Otherwise, your code, as you posted it, is working fine in this jsFiddle (jsFiddle uses HTML5, which is why it works here).

http://jsfiddle.net/bpmtf/

Can anyone please send me a link where I can download a proper .css stylesheet

You'd write your own CSS to style the page elements & layout, otherwise, CSS has absolutely nothing to do with how this plugin operates.

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285