我正在使用jquery进行表单验证所有字段都正常工作但只有checkbox
和radio button
没有验证,并且两个单选按钮都会被检查我应该怎样做才能有人建议我根据我的代码在哪里是问题。
如果表格中的任何错误随时可以告诉。
$(document).ready(function() {
$('.success_msg').hide();
/* name*/
$('#contact_name').on('input', function() {
var input=$(this);
var regex = /^([^0-9]*)$/;
var is_name=regex.test(input.val());
if(is_name){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* E-mail */
$('#contact_email').on('input', function() {
var input=$(this);
var regex = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
var is_email=regex.test(input.val());
if(is_email){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* Phone */
$('#contact_phone').on('input', function() {
var input=$(this);
var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
var is_phone=regex.test(input.val());
if(is_phone){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* message*/
$('#contact_message, #contact_enquiry').keyup(function(event) {
var input=$(this);
var message=$(this).val();
if(message){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* Radio button */
$('#contact_male, #contact_female').click(function(){
if($(this).is(":checked")){
$(this).removeClass("invalid").addClass("valid");
}
else if($(this).is(":not(:checked)")){
$(this).removeClass("valid").addClass("invalid");
}
});
/* Checkbox */
// $('#contact_check').click(function(){
// if($(this).is(":checked")){
// $(this).removeClass("invalid").addClass("valid");
// }
// else if($(this).is(":not(:checked)")){
// $(this).removeClass("valid").addClass("invalid");
// }
// });
/* Submit */
$("#submit_form").click(function(event){
var form_data = $("#contact").serializeArray();
var error_free = true;
for (var input in form_data){
var element = $("#contact_"+form_data[input]['name']);
var valid = element.hasClass("valid");
var error_element = $("span", element.parent());
if (!valid){
error_element.removeClass("error").addClass("error_show"); error_free=false;
}
else{
error_element.removeClass("error_show").addClass("error");
}
}
if (!error_free){
event.preventDefault();
}
else{
$('.success_msg').fadeIn().delay(3000).fadeOut();
$('input , textarea , select').val('').removeClass('valid');
event.preventDefault();
}
});
});

.success_msg {
width: 350px;
line-height: 40px;
border: 1px solid green;
border-radius: 5px;
background-color: rgba(213, 255, 187, 0.7);
display: none;
position: absolute;
bottom: 5px;
left: 50%;
transform: translateX(-50%);
z-index: 999;
text-align: center;
}
.iva-nav {
width: 100px;
text-align: center;
color: #fff;
font-size: 16px;
font-weight: 600;
transition: all 0.74s ease;
margin: 10px;
padding: 10px;
cursor: pointer;
position: relative;
transition: all .7s ease-in-out;
}
.iva-nav span {
transform: translateZ(8px);
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
position: relative;
z-index: 999;
}
.iva-nav:before {
content: '';
position: absolute;
padding: 10px;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(254, 180, 29, 0.3);
box-sizing: border-box;
border: 1px solid rgba(255, 225, 225, 0.4);
transition: all 0.5s ease-in-out;
z-index: 50;
}
.iva-nav:after {
content: '';
position: absolute;
padding: 10px;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(254, 180, 29, 0.3);
box-sizing: border-box;
border: 1px solid rgba(255, 225, 225, 0.4);
transition: all 0.6s ease-in-out;
z-index: 25;
}
.iva-nav:hover:before {
transform: rotate(225deg);
background-color: rgba(254, 180, 29, 0.6);
border: 1px solid rgba(255, 225, 225, 0.1);
}
.iva-nav:hover:after {
transform: rotate(133deg);
background-color: rgba(254, 180, 29, 0.6);
border: 1px solid rgba(255, 225, 225, 0.1);
}
.fancy-heading-s1 {
max-width: 1400px;
width: 100%;
margin: 0 auto;
font-size: 40px;
padding: 25px 0;
color: #ffffff;
}
.fancy-heading-s2 {
font-size: 30px;
margin: 20px 0;
color: #fff;
}
.fancy-title {
display: block;
font-size: 13px;
margin: 20px 0;
font-weight: 400;
color: #ffffff;
}
.cfar-s1 .cfar-heading {
text-align: center;
}
*:focus{ outline: none;}
.cfar {
max-width: 1140px;
width: 100%;
margin: 0 auto;
position: relative;
box-sizing: border-box;
}
.cfar-s1 {
padding: 40px 100px;
background-color: #313439;
}
.cfar-s1 .fancy-title span {
position: relative;
text-transform: uppercase;
cursor: pointer;
}
.cfar-s1 .fancy-title span:after {
content: " . ";
position: relative;
top: 50%;
display: inline-block;
transform: translateY(-15%);
margin: 0 10px;
}
.cfar-s1 .fancy-title span:last-child:after {
content: "";
margin-right: 0;
}
.cfar-s1 .fancy-heading-s2 h2 {
font-size: 40px;
font-weight: bold;
text-transform: uppercase;
}
.cfar-s1 .fancy-heading-s2 h2 span {
color: #da4e5b;
}
.cfar-s1 form {
padding: 30px 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.cfar .detail {
position: relative;
margin: 20px 0;
flex-basis: 48%;
}
.cfar .detail input,.detail textarea,.detail select {
display: block;
border: none;
border-bottom: 1px solid #fff;
padding: 5px 0;
width: 100%;
background: none;
color: #ffffff;
}
.cfar option { background: rgba(0,0,0,0.8); }
.cfar .detail textarea {
height: 100px;
width: 100%;
overflow: auto;
resize: none;
}
.cfar .detail label {
position: absolute;
top: 0px;
left: 5px;
font-size: 16px;
pointer-events: none;
transition: all 0.43s ease-in-out;
}
.cfar .detail .inputBar {
position: relative;
display: block;
transition: all 0.43s ease-in-out;
}
.cfar .detail input:focus:after,.detail textarea:foucs:after,.detail select:focus:after,
.cfar .detail .inputBar:focus:before,
.cfar .detail .inputBar:focus:after {
content: "";
display: block;
position: absolute;
bottom: 0;
width: 0;
background: red;
height: 2px;
}
.cfar .detail .inputBar:after { right: 50%;}
.cfar .detail .inputBar:before { left: 50%;}
.cfar .detail input:focus ~ label,
.cfar .detail input:valid ~ label,
.cfar .detail textarea:focus ~ label,
.cfar .detail textarea:valid ~ label {
top: -20px;
transition: all 0.3s ease-in-out;
font-size: 13px;
}
.cfar .detail input:focus ~ .inputBar:before,
.cfar .detail input:focus ~ .inputBar:after,
.cfar .detail textarea:focus ~ .inputBar:before,
.cfar .detail textarea:focus ~ .inputBar:after
.cfar .detail select:focus ~ .inputBar:before,
.cfar .detail select:focus ~ .inputBar:after {
width: 50%;
}
.cfar .detail .answer {
display: inline-block;
width: 114px;
height: 20px;
border: 1px solid #ffffff;
left: 133px;
position: inherit;
}
/* custom css*/
span.error{
/*display: none;*/
color: red;
opacity: 0;
position: absolute;
top: 10px !important;
right: 0;
transition: all 0.4s ease-in-out;
}
.error_show {
position: absolute;
top: 0;
right: 0px;
color: red;
}
input:focus {
outline-width: 0;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 0;
background-color: none;
}
/*-------------------------------------------------------------
contact form style 1
-------------------------------------------------------------- */
.cfar-s1 .detail input,.cfar-s1 .detail textarea,.cfar-s1 .detail select {
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
position: relative;
font-size: 20px;
font-family: 'Roboto Slab', serif;
position: relative;
z-index: 99;
}
.cfar-s1 .detail .inputBar {
position: relative;
}
.cfar-s1 .detail .inputBar:before {
content: "";
height: 1px;
position: absolute;
left: 0;
top: -1px;
background-color: #ff0000;
width: 0;
transition: all 0.4s ease-in-out;
}
.cfar-s1 .detail input:focus ~ .inputBar:before,
.cfar-s1 .detail textarea:focus ~ .inputBar:before {
width: 100%;
}
.cfar-s1 .detail label {
top: 0;
font-size: 14px;
color: #777777;
}
.message {
flex-basis: 100% !important;
}
.btn-container {
margin: 40px auto;
}
.btn-container .btn {
border: none;
background-color: #da4e5b;
padding: 20px 75px;
color: #fff;
width: 100%;
font-size: 14px;
font-weight: bold;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 2px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="contactform1">
<div id="note"></div>
<form id="contact" method="post" action="">
<div id="note"></div>
<div class="detail">
<input type="text" id="contact_name" name="name" autocomplete="off" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_name">Name</label>
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail">
<input type="text" id="contact_email" name="email" autocomplete="off" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_email">Email</label>
<span class="error">A valid email address is required</span>
</div><!--detail-->
<div class="detail">
<input type="number" id="contact_phone" autocomplete="off" name="phone" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_phone">Phone Number</label>
<span class="error">A valid Phone number is required</span>
</div><!--detail-->
<div class="detail">
<input type="text" id="contact_enquiry" autocomplete="off" name="enquiry" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_enquiry">I want to know about...</label>
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail message">
<textarea type="text" id="contact_message" autocomplete="off" name="message" required></textarea>
<span class="inputBar"></span><!--inputBar-->
<label for="contact_message">Type your Message here</label>
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail">
<span class="inputBar"></span><!--inputBar-->
<input type="radio" id="contact_male" name="male" required />Male
<input type="radio" id="contact_female" name="female" required />Female
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail">
<span class="inputBar"></span><!--inputBar-->
<input type="checkbox" id="contact_html" name="html" required />HTMl
<input type="checkbox" id="contact_css" name="css" required />CSS
<span class="error">This field is required</span>
</div><!--detail-->
<div class="btn-container" id="submit_form">
<a href="#">Submit</a>
</div>
</form>
</div>
&#13;
答案 0 :(得分:0)
它们(单选按钮)必须具有相同的名称。将名称命名为gender
并添加值属性以获取每个类似
$(document).ready(function() {
$('.success_msg').hide();
/* name*/
$('#contact_name').on('input', function() {
var input=$(this);
var regex = /^([^0-9]*)$/;
var is_name=regex.test(input.val());
if(is_name){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* E-mail */
$('#contact_email').on('input', function() {
var input=$(this);
var regex = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
var is_email=regex.test(input.val());
if(is_email){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* Phone */
$('#contact_phone').on('input', function() {
var input=$(this);
var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
var is_phone=regex.test(input.val());
if(is_phone){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* message*/
$('#contact_message, #contact_enquiry').keyup(function(event) {
var input=$(this);
var message=$(this).val();
if(message){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
/* Radio button */
$('#contact_male, #contact_female').click(function(){
if($(this).is(":checked")){
$(this).removeClass("invalid").addClass("valid");
}
else if($(this).is(":not(:checked)")){
$(this).removeClass("valid").addClass("invalid");
}
});
/* Checkbox */
// $('#contact_check').click(function(){
// if($(this).is(":checked")){
// $(this).removeClass("invalid").addClass("valid");
// }
// else if($(this).is(":not(:checked)")){
// $(this).removeClass("valid").addClass("invalid");
// }
// });
/* Submit */
$("#submit_form").click(function(event){
var form_data = $("#contact").serializeArray();
var error_free = true;
for (var input in form_data){
var element = $("#contact_"+form_data[input]['name']);
var valid = element.hasClass("valid");
var error_element = $("span", element.parent());
if (!valid){
error_element.removeClass("error").addClass("error_show"); error_free=false;
}
else{
error_element.removeClass("error_show").addClass("error");
}
}
if (!error_free){
event.preventDefault();
}
else{
$('.success_msg').fadeIn().delay(3000).fadeOut();
$('input , textarea , select').val('').removeClass('valid');
event.preventDefault();
}
});
});
.success_msg {
width: 350px;
line-height: 40px;
border: 1px solid green;
border-radius: 5px;
background-color: rgba(213, 255, 187, 0.7);
display: none;
position: absolute;
bottom: 5px;
left: 50%;
transform: translateX(-50%);
z-index: 999;
text-align: center;
}
.iva-nav {
width: 100px;
text-align: center;
color: #fff;
font-size: 16px;
font-weight: 600;
transition: all 0.74s ease;
margin: 10px;
padding: 10px;
cursor: pointer;
position: relative;
transition: all .7s ease-in-out;
}
.iva-nav span {
transform: translateZ(8px);
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
position: relative;
z-index: 999;
}
.iva-nav:before {
content: '';
position: absolute;
padding: 10px;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(254, 180, 29, 0.3);
box-sizing: border-box;
border: 1px solid rgba(255, 225, 225, 0.4);
transition: all 0.5s ease-in-out;
z-index: 50;
}
.iva-nav:after {
content: '';
position: absolute;
padding: 10px;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(254, 180, 29, 0.3);
box-sizing: border-box;
border: 1px solid rgba(255, 225, 225, 0.4);
transition: all 0.6s ease-in-out;
z-index: 25;
}
.iva-nav:hover:before {
transform: rotate(225deg);
background-color: rgba(254, 180, 29, 0.6);
border: 1px solid rgba(255, 225, 225, 0.1);
}
.iva-nav:hover:after {
transform: rotate(133deg);
background-color: rgba(254, 180, 29, 0.6);
border: 1px solid rgba(255, 225, 225, 0.1);
}
.fancy-heading-s1 {
max-width: 1400px;
width: 100%;
margin: 0 auto;
font-size: 40px;
padding: 25px 0;
color: #ffffff;
}
.fancy-heading-s2 {
font-size: 30px;
margin: 20px 0;
color: #fff;
}
.fancy-title {
display: block;
font-size: 13px;
margin: 20px 0;
font-weight: 400;
color: #ffffff;
}
.cfar-s1 .cfar-heading {
text-align: center;
}
*:focus{ outline: none;}
.cfar {
max-width: 1140px;
width: 100%;
margin: 0 auto;
position: relative;
box-sizing: border-box;
}
.cfar-s1 {
padding: 40px 100px;
background-color: #313439;
}
.cfar-s1 .fancy-title span {
position: relative;
text-transform: uppercase;
cursor: pointer;
}
.cfar-s1 .fancy-title span:after {
content: " . ";
position: relative;
top: 50%;
display: inline-block;
transform: translateY(-15%);
margin: 0 10px;
}
.cfar-s1 .fancy-title span:last-child:after {
content: "";
margin-right: 0;
}
.cfar-s1 .fancy-heading-s2 h2 {
font-size: 40px;
font-weight: bold;
text-transform: uppercase;
}
.cfar-s1 .fancy-heading-s2 h2 span {
color: #da4e5b;
}
.cfar-s1 form {
padding: 30px 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.cfar .detail {
position: relative;
margin: 20px 0;
flex-basis: 48%;
}
.cfar .detail input,.detail textarea,.detail select {
display: block;
border: none;
border-bottom: 1px solid #fff;
padding: 5px 0;
width: 100%;
background: none;
color: #ffffff;
}
.cfar option { background: rgba(0,0,0,0.8); }
.cfar .detail textarea {
height: 100px;
width: 100%;
overflow: auto;
resize: none;
}
.cfar .detail label {
position: absolute;
top: 0px;
left: 5px;
font-size: 16px;
pointer-events: none;
transition: all 0.43s ease-in-out;
}
.cfar .detail .inputBar {
position: relative;
display: block;
transition: all 0.43s ease-in-out;
}
.cfar .detail input:focus:after,.detail textarea:foucs:after,.detail select:focus:after,
.cfar .detail .inputBar:focus:before,
.cfar .detail .inputBar:focus:after {
content: "";
display: block;
position: absolute;
bottom: 0;
width: 0;
background: red;
height: 2px;
}
.cfar .detail .inputBar:after { right: 50%;}
.cfar .detail .inputBar:before { left: 50%;}
.cfar .detail input:focus ~ label,
.cfar .detail input:valid ~ label,
.cfar .detail textarea:focus ~ label,
.cfar .detail textarea:valid ~ label {
top: -20px;
transition: all 0.3s ease-in-out;
font-size: 13px;
}
.cfar .detail input:focus ~ .inputBar:before,
.cfar .detail input:focus ~ .inputBar:after,
.cfar .detail textarea:focus ~ .inputBar:before,
.cfar .detail textarea:focus ~ .inputBar:after
.cfar .detail select:focus ~ .inputBar:before,
.cfar .detail select:focus ~ .inputBar:after {
width: 50%;
}
.cfar .detail .answer {
display: inline-block;
width: 114px;
height: 20px;
border: 1px solid #ffffff;
left: 133px;
position: inherit;
}
/* custom css*/
span.error{
/*display: none;*/
color: red;
opacity: 0;
position: absolute;
top: 10px !important;
right: 0;
transition: all 0.4s ease-in-out;
}
.error_show {
position: absolute;
top: 0;
right: 0px;
color: red;
}
input:focus {
outline-width: 0;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 0;
background-color: none;
}
/*-------------------------------------------------------------
contact form style 1
-------------------------------------------------------------- */
.cfar-s1 .detail input,.cfar-s1 .detail textarea,.cfar-s1 .detail select {
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
position: relative;
font-size: 20px;
font-family: 'Roboto Slab', serif;
position: relative;
z-index: 99;
}
.cfar-s1 .detail .inputBar {
position: relative;
}
.cfar-s1 .detail .inputBar:before {
content: "";
height: 1px;
position: absolute;
left: 0;
top: -1px;
background-color: #ff0000;
width: 0;
transition: all 0.4s ease-in-out;
}
.cfar-s1 .detail input:focus ~ .inputBar:before,
.cfar-s1 .detail textarea:focus ~ .inputBar:before {
width: 100%;
}
.cfar-s1 .detail label {
top: 0;
font-size: 14px;
color: #777777;
}
.message {
flex-basis: 100% !important;
}
.btn-container {
margin: 40px auto;
}
.btn-container .btn {
border: none;
background-color: #da4e5b;
padding: 20px 75px;
color: #fff;
width: 100%;
font-size: 14px;
font-weight: bold;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="contactform1">
<div id="note"></div>
<form id="contact" method="post" action="">
<div id="note"></div>
<div class="detail">
<input type="text" id="contact_name" name="name" autocomplete="off" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_name">Name</label>
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail">
<input type="text" id="contact_email" name="email" autocomplete="off" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_email">Email</label>
<span class="error">A valid email address is required</span>
</div><!--detail-->
<div class="detail">
<input type="number" id="contact_phone" autocomplete="off" name="phone" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_phone">Phone Number</label>
<span class="error">A valid Phone number is required</span>
</div><!--detail-->
<div class="detail">
<input type="text" id="contact_enquiry" autocomplete="off" name="enquiry" required />
<span class="inputBar"></span><!--inputBar-->
<label for="contact_enquiry">I want to know about...</label>
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail message">
<textarea type="text" id="contact_message" autocomplete="off" name="message" required></textarea>
<span class="inputBar"></span><!--inputBar-->
<label for="contact_message">Type your Message here</label>
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail">
<span class="inputBar"></span><!--inputBar-->
<input type="radio" id="contact_male" value="male" name="gender" required />Male
<input type="radio" id="contact_female" value="female" name="gender" required />Female
<span class="error">This field is required</span>
</div><!--detail-->
<div class="detail">
<span class="inputBar"></span><!--inputBar-->
<input type="checkbox" id="contact_html" name="html" required />HTMl
<input type="checkbox" id="contact_css" name="css" required />CSS
<span class="error">This field is required</span>
</div><!--detail-->
<div class="btn-container" id="submit_form">
<a href="#">Submit</a>
</div>
</form>
</div>
答案 1 :(得分:0)
尝试使用相同的名称,这样就无法同时点击它们。
答案 2 :(得分:0)
问题在于您要单独验证所有复选框和单选按钮,而您将它们放在一起非常明显:1个radiobutton的有效性取决于另一个单选按钮的检查属性。
为了解决这个问题,您需要创建一个验证函数来计算属于一起的所有单选按钮,并将checked-property计为1。
更改radiobuttons和checkbuttons的HTML,使其具有包装类:
<div class="detail gender">
span class="inputBar"></span><!--inputBar-->
<input type="radio" value="male" name="gender" />Male
<input type="radio" value="female" name="gender" />Female
<span class="error">This field is required</span>
</div><!--detail-->
注意&#34;性别&#34;包裹radiobuttons的类。还要注意两个单选按钮是如何具有相同的名称(这将在浏览器中将无线电按钮组合在一起,即如果您选择一个,它将取消选择另一个)。当为&#34;性别&#34;提交时,value属性将与表单一起发送。字段。
现在,您可以通过在给定时间执行此操作来验证这些按钮中的至少一个(例如,在单击事件中或可能在提交表单时)
var radiobuttons = $(".gender").find("input[type=radio]");
if(radiobuttons.prop("checked").length === 1) {
// do valid stuff here
} else {
// do invalid stuff here
}
这也适用于复选框。