jquery.validate与jquery-1.4.3无法正常工作

时间:2012-08-24 19:08:38

标签: jquery jquery-validate

以下脚本将在取消聚焦时显示错误消息。我将它更改为jQuery 4.3或更新版本,但它没有这样做。我知道我在做一些愚蠢的事,但找不到它。请提供建议。感谢

<!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=ISO-8859-1" />
        <title>jQuery validation</title>
        <script src="http://code.jquery.com/jquery-1.4.2.js"></script> 
        <script src="jquery.validate.js" type="text/javascript"></script>
        <script src="additional-methods.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(function(){
                var validate = $("#signupForm").validate({
                    rules: {phone:'phoneUS'},
                    messages: {phone:'Enter a valid phone number'},
                    submitHandler: function(form) {alert('submit');}
                });

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

        <form id="signupForm" method="get" action="">
            <label for="phone">phone</label>
            <input id="phone" name="phone" />
            <input type="submit" value="Submit" class="submit" />
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

phoneUS方法在additional-methods.js中定义。如果您包含此文件(并确保您的输入有type='text'),它应该可以正常工作:

<强> JavaScript的:

$("#signupForm").validate({
    rules: {
        phone: 'phoneUS'
    },
    messages: {
        phone: 'Enter a valid phone number'
    },
    submitHandler: function(form) {
        alert('submit');
    }
});

<强> HTML:

<form id="signupForm" method="get" action="">
    <label for="phone">phone</label>
    <input id="phone" name="phone" type="text"/>
    <input type="submit" value="Submit" class="submit" />
</form>​​​​​​​​​​​​​​​

示例: http://jsfiddle.net/kzDnx/5/