【问题标题】:how to validate inputs in ajax form如何验证ajax形式的输入
【发布时间】:2012-10-01 17:21:03
【问题描述】:

我有这个带有姓名、电子邮件和消息的 ajax 表单,我想验证输入:

<script type="text/javascript">
$(document).ready(function(){
   $form = $('form');
   $form.submit(function(){
         $.post($(this).attr('action'), $(this).serialize(), function(response){
      $("#inlocuire").fadeOut(1000, function () {
            $(this).html("<img src='images/de_multumire.png'/>").fadeIn(2000);
        });

        var message = $('textarea[name=message]').val('');
        var name = $('input[name=name]').val('');
        var email = $('input[name=email]').val('');
      },'json');
      return false;
   });

});

我尝试引入这个功能,但它不起作用:

$('#form_id').ajaxForm({
        beforeSubmit: validate
    });

    function validate(formData, jqForm, options) {
        var name = $('input[name=name]').fieldValue();
        var email = $('input[name=email]').fieldValue();
        var message = $('textarea[name=message]').fieldValue();

        if (!name[0]) {
            alert('Please enter a value for name');
            return false;
        }
        if (!email[0]) {
            alert('Please enter a value for email');
            return false;
        }
        if (!message[0]) {
            alert('Please enter a value for message');
            return false;
        }

        else {
        // here to send the form
                      }

表单工作正常...

【问题讨论】:

标签: ajax forms validation


【解决方案1】:

对于和我有同样问题的人:http://docs.jquery.com/Plugins/Validation

【讨论】:

    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多