0

我无法验证由姓名和电子邮件字段组成的简单表单,并且无法显示自定义错误消息。电子邮件错误消息似乎有效,但对于名称,我得到“此字段是必需的”。而不是我指定的那个。

表单 HTML:

你的名字:

<div class="right"><input type="text" id="alert_name" required/></div>

<p>&nbsp;</p>

<div class="left">Your Email:</div>

<div class="right"><input type="email" id="alert_email" required/></div>

<div class="clear:both;">&nbsp;</div>
<p id="errors" style="clear: both; margin-top:15px; text-align: center; color: red;">&nbsp;</p>
</form>

jQuery:

$( "#basic-modal-content" ).dialog({ autoOpen: false, width: 400, buttons: [ { text: "Ok", click: function() {

            $("#email_alert_form").validate({
                errorLabelContainer: '#errors',
                messages: {
                    email: {
                        required: "Please enter an email address",
                        email: "Please enter a valid email address"
                    },
                    alert_name: 
                        required: "Please enter your name"
                    }
                }
            });

            if (!$('#email_alert_form').valid()){

            } else {
                var datastring = 'email_alert=' + 'true' + '&name='+ $('#alert_name').val() + '&email=' + $('#alert_email').val();

                $.ajax({  
                  type: "POST",  
                  url: "process-contact.php",  
                  data: datastring,  
                  success: function(data) {  
                     $( "#submitted_alert" ).dialog( "open" );
                  }  
                });  
                $( this ).dialog( "close" );
            }   
        }
    }
]

});

任何帮助将非常感激。

4

1 回答 1

2

您的代码中几乎没有语法错误...希望这是复制粘贴错误...

您需要为name输入字段赋予属性...

<input type="text" name="alert_name" required />

<input type="email" name="alert_email" required />

演示:小提琴

于 2013-09-28T17:31:57.453 回答