如何使用jquery.validate执行验证?

时间:2014-02-21 18:25:29

标签: javascript jquery validation

我正在尝试使用jquery.validate来验证jquery模式对话框中的某些字段。练习我想我会创建一个简单的JSFiddle来确保我的语法是正确的。我错过了一些东西,希望有人可以提供帮助。

这是我的简单形式:

<form id="myform" method="post" action="#">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="submit" />
</form>

这是我的jquery:

$("#submitEdit").click(function (event) {
    event.preventDefault();
Validate();
});

function Validate()
{       
    $("#frmEdit").validate({
        rules: {
            QtyOnHand: {
                required: true
            }
        },
        messages: {
            QtyOnHand: {
                required: 'Qty Required'
            }

        }
  });
}

2 个答案:

答案 0 :(得分:1)

<form id="myform" method="post" action="#">
      <input type="text" name="field1" />
      <input type="text" name="field2" />
      <input type="submit" />
   </form>

JS:

$("#myForm").validate({
        rules: {
            field1: {
                required: true
            },
            field2: {
                required: true
            }
        },
        messages: {
            field1: {
                required: 'field1 Required'
            },
            field2: {
                required: 'field2 Required'
            }

        }
  });

检查 Fiddle

答案 1 :(得分:1)

Working Fiddle Demo

保持

$("#frmEdit").validate({//this will validate you don't need call validate function
    rules: {
        QtyOnHand: {
            required: true
        }
    },
    messages: {
        QtyOnHand: {
            required: 'Qty Required'
        }
    }
});

<小时/> 除去

$("#submitEdit").click(function (event) {
    event.preventDefault();//stopping validation to occur
}

Problem Fiddle