为什么我的验证员不工作?

时间:2010-04-08 08:01:45

标签: jquery validation

任何人都可以告诉我为什么以下代码不起作用?

<script type="text/javascript" src="../../Scripts/jquery.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.js"></script>
    <script type="text/javascript">
    $(function() {
        // validate contact form on keyup and submit
        $("#myform").validate({
            //set the rules for the fild names
            rules: {
                hour: {
                    required: true,
                    minlength: 2,
                    range:[0,23]
                },
                minute: {
                    required: true,
                    minlength: 2,
                    range:[0,60]
                },
            },
            //set messages to appear inline
            messages: {
                hour: "Please enter a valid hour",
                minute: "Please enter a valid minute"
            }
        });

    });
</script>
 <style type="text/css">
.error {
    color: red;
    font: 12pt verdana;
    padding-left: 10px
}
</style>

   <form id="myform" method="" action="">

   <input id="hour" type="text" name="hour" style="width:30px; text-align:center;"></input> :

    <input id="minute" type="text" name="minute" style="width:30px; text-align:center;"></input>
  <br/>
  <input type="submit" value="Validate!" />
</form>

提前感谢一百万, 丽娜

2 个答案:

答案 0 :(得分:1)

我看不出你的代码有什么问题。确保包含的脚本的地址正确无误。这是一个使用来自Microsoft CDN的jquery和jquery.validate的工作示例:

<!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>test</title>
    <style type="text/css">
    .error {
        color: red;
        font: 12pt verdana;
        padding-left: 10px
    }
    </style>
</head>
<body>
   <form id="myform" action="">
       <input id="hour" type="text" name="hour" style="width:30px; text-align:center;" /> :
       <input id="minute" type="text" name="minute" style="width:30px; text-align:center;" />
       <br/>
       <input type="submit" value="Validate!" />
    </form>

    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.min.js"></script>
    <script type="text/javascript">
    $(function() {
        // validate contact form on keyup and submit
        $("#myform").validate({
            //set the rules for the fild names
            rules: {
                hour: {
                    required: true,
                    minlength: 2,
                    range:[0,23]
                },
                minute: {
                    required: true,
                    minlength: 2,
                    range:[0,60]
                },
            },
            //set messages to appear inline
            messages: {
                hour: "Please enter a valid hour",
                minute: "Please enter a valid minute"
            }
        });

    });
    </script>
</body>
</html>

答案 1 :(得分:0)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Simple Form Validation</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#myform").validate({
        //set the rules for the fild names
        rules: {
            hour: {
                required: true,
                minlength: 2,
                range:[0,23]
            },
            minute: {
                required: true,
                minlength: 2,
                range:[0,60]
            },
        },
        //set messages to appear inline
        messages: {
            hour: "Please enter a valid hour",
            minute: "Please enter a valid minute"
        }
    });
});
</script>

<style type="text/css">
.error {
    color: red;
    font: 12pt verdana;
    padding-left: 10px
}
</style>
</head>

<body>
    <form id="myform" method="" action="">
        <input id="hour" type="text" name="hour" style="width:30px; text-align:center;"></input>
        <input id="minute" type="text" name="minute" style="width:30px; text-align:center;"></input>
        <br/>
        <input type="submit" value="Validate!" />
    </form>
</body>
</html>

正在运作