我正在使用jQuery进行表单验证我已经对每个输入字段使用了id's
。现在我想添加jquery typeof()
方法,如果字段是文本,textarea应该在一个循环中验证,如此电话号码和电子邮件等等。我想用if / else条件写它。这个表格工作正常,除了收音机和复选框没有被验证(提交时应该抛出错误)。但现在我想完全改变我的代码,如上所述。随意告诉是否有任何错误。而且我不想使用任何外部插件。
任何人都可以建议我如何实现?
任何帮助将不胜感激
$(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.1/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;