gpt4 book ai didi

javascript - 如何在 jQuery-validate 中指定验证字段

转载 作者:行者123 更新时间:2023-11-30 19:10:18 24 4
gpt4 key购买 nike

我有 2 个字段:Name Query 和 Date of Birth,而 Name Query 是必填字段,而 DoB 不是。我有以下表单在名称查询字段下显示验证消息:

var myForm = $("#myform").validate({
rules: {
NameQuery: "required",
DoBQuery: {
required: false
}
},
messages: {
NameQuery: "Please fill in name query field",
DoBQuery: ""
}
});
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>


<form id="myform" method="post" action="">
<div>
<label for="NameQuery">Name</label>
<input type="text" id="NameQuery" name="NameQuery">
</div>
<div>
<label for="DoBQuery">Date of Birth</label>
<input type="text" id="DoBQuery" name="DoBQuery">
</div>
<input type="submit" value="Search">
</form>

现在我想为名称查询字段验证消息添加一些样式。代码片段中添加了一些代码,请注意 <div class="error__input"></div>必须EXIST IN BOTH FIELDS就像在片段中一样

var myForm = $("#myform").validate({
rules: {
NameQuery: "required",
DoBQuery: {
required: false
}
},
messages: {
NameQuery: "Please fill in name query field",
DoBQuery: ""
},
errorElement : 'div',
errorLabelContainer: '.error__input'
});
.error__input{
min-height: 20px;
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form id="myform" method="post" action="">
<div>
<label for="NameQuery">Name</label>
<input type="text" id="NameQuery" name="NameQuery">
<div class="error__input"></div>
</div>
<div>
<label for="DoBQuery">Date of Birth</label>
<input type="text" id="DoBQuery" name="DoBQuery">
<div class="error__input"></div>
</div>
<input type="submit" value="Search">
</form>

现在验证消息存在于两个字段中,我知道 jQuery-validate 找到了 error__input div 并将该类应用于它,但为什么它会将消息添加到第二个字段以及如何将警报消息设置为仅适用于第一条消息,同时 <div class="error__input"></div>一定要去吗?

最佳答案

我猜你想要这样的东西:

var myForm = $("#myform").validate({
rules: {
NameQuery: "required",
DoBQuery: {
required: false
}
},
messages: {
NameQuery: "Please fill in name query field",
DoBQuery: ""
}
});
#myform .error {
display: block;
color: red;
}

#myform input {
display: inline;
}

#myform > div {
height: 50px;
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>


<form id="myform" method="post" action="">
<div>
<label for="NameQuery">Name</label>
<input type="text" id="NameQuery" name="NameQuery" style="display: inline-block;">
</div>
<div>
<label for="DoBQuery">Date of Birth</label>
<input type="text" id="DoBQuery" name="DoBQuery" style="display: inline-block;">
</div>
<input type="submit" value="Search">
</form>

关于javascript - 如何在 jQuery-validate 中指定验证字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58557124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com