minlength输入字段验证 - JQuery

时间:2016-03-28 14:34:26

标签: javascript jquery html cordova validation

我在下面有JQuery验证,只是确保在输入字段中写入了某些内容,我在html中也有maxlength。

但是,我希望有人可以对这种情况有所了解,并告诉我如何在下面的jquery代码中添加某种类型的minlength,这样我就不需要做任何额外的功能了?

 $('#form1').submit(function (e) {
    e.preventDefault();
}).validate({
    rules: {
        txtusername: {
            required: true
        },
        txtfirstname: {
            required: true
        },
        txtemail: {
            required: true
        },
        txtpassword: {
            required: true
        },
        passwordconfirm: {
            required: true
        }
    },
    messages: {
        txtusername: {
            required: "Please enter your Username."
        },
        txtfirstname: {
            required: "Please enter your First Name."
        },
        txtemail: {
            required: "Please enter your Email."
        },
        txtpassword: {
            required: "Please enter your Password."
        },
        passwordconfirm: {
            required: "Please enter your password again."
        }
    },
    errorPlacement: function (error, element) {
        error.appendTo(element.parent().prev());
    },
    submitHandler: function (form, user) {

        CheckUser(form);
        return false;
    }
});

示例HTML -

<div data-role="fieldcontainer">
            <label for="txtusername" data-theme="d">Username:</label>
            <input type="text" id="txtusername" name="txtusername" maxlength="12"  placeholder="Enter Username"/>
        </div>

1 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

rules:{
    txtusername: {
        required: true,
        minlength: 3
    },
}