gpt4 book ai didi

jquery - 验证插件未验证所有字段

转载 作者:行者123 更新时间:2023-12-01 02:11:18 29 4
gpt4 key购买 nike

我正在使用 jQuery 验证插件来进行表单验证。它只是检查第一个字段。如果第一个字段验证为真,则正在提交表单。它不验证其余字段..

我正在使用 bootstrap 3.0

  <script type="text/javascript"> 
$(document).ready(function(){
$('#contact').validate({
rules: {
firstname: {
required: true,
minlength: 5,
},
useremail: {
required: true,
}
}
});
})
</script>
<form action="" method="" id="contact">
<div class="form-group">
<label> Your Name </label>
<input type="text" class="form-control" placeholder="Enter Your Name" id="firstname" required="" minlength="5"/>
</div>

<div class="form-group">
<label> Your E-mail </label>
<input type="email" class="form-control" placeholder="Enter Your Email" id="useremail" required=""/>
</div>

<div class="form-group">
<label> Contact Detail </label>
<input type="text" class="form-control" placeholder="Enter Contact Number" id="mobile" minlength="10" maxlength="10" required=""/>
</div>
<div class="form-group">
<label> Type</label>
<select class="form-control">
<option> Inquiry </option>
<option> Complaints </option>
<option> Feedback </option>
<option> Others </option>
</select>
</div>
<div class="form-group">
<label> Message</label>
<textarea class="form-control" rows="3" cols="5" placeholder="Enter Your Message Here..." id="message" minlength='10' required=""></textarea>
</div>
<button type="submit" class="btn btn-primary btn-lg"> Send</button>
</form>

我不知道我的代码出了什么问题?请帮忙...

提前致谢

最佳答案

您的验证选项中有拼写错误。规则必须是规则。

还必须指定名称属性:

HTML

<form action="" method="" id="contact">
<div class="form-group">
<label>Your Name</label>
<input type="text" class="form-control" placeholder="Enter Your Name" name="firstname" required="" minlength="5" />
</div>
<div class="form-group">
<label>Your E-mail</label>
<input type="email" class="form-control" placeholder="Enter Your Email" name="useremail" required="" />
</div>
<div class="form-group">
<label>Contact Detail</label>
<input type="text" class="form-control" placeholder="Enter Contact Number" name="mobile" minlength="10" maxlength="10" required="" />
</div>
<div class="form-group">
<label>Type</label>
<select class="form-control">
<option>Inquiry</option>
<option>Complaints</option>
<option>Feedback</option>
<option>Others</option>
</select>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" rows="3" cols="5" placeholder="Enter Your Message Here..." id="message" minlength='10' required=""></textarea>
</div>
<button type="submit" class="btn btn-primary btn-lg">Send</button>
</form>

Javascript

$(document).ready(function () {
$('#contact').validate({
rules: {
firstname: {
required: true,
minlength: 5,
},
useremail: {
required: true,
}
},
errorClass : 'has-error',
validClass : 'has-success',
highlight:function(element, errorClass, validClass) {
$(element).parents('div').addClass(errorClass).children().removeClass(errorClass);
$(element).parents('div').removeClass(validClass).children().removeClass(validClass);
},
unhighlight: function(element, errorClass, validClass) {
$(element).parents('div').removeClass(errorClass).children().removeClass(errorClass);
$(element).parents('div').addClass(validClass).children().removeClass(validClass);
}
});
})

fiddle here

可以使用此 CSS 来完成错误和有效的设置:

.has-error
{
background-color: rgb(255, 0, 0);
}
.has-success
{
background-color: rgb(0, 255, 0);
}

关于jquery - 验证插件未验证所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19541917/

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