我正在使用jQuery进行表单验证。我想使用if / else条件验证完整的表单,我不想使用任何插件。例如:
$('.submit').click(function(){
//validation here all form fields
});
我想写一个像依赖type
属性文本,电子邮件等的条件。
我的问题是,有人可以建议我怎么做吗?
$(document).ready(function(){
$(#submit_form).on('click', function(){
//validate here
});
});
.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" 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" 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" 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" 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" 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" name="male" required />Male
<input type="radio" 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" name="html" required />HTMl
<input type="checkbox" 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>
答案 0 :(得分:0)
试试这种方式
$('.submit').click(function(){
if($("#form_id").valid()){
return true
}else{
alert(" Enter Valid Data");
return false;
}
});
&#13;
答案 1 :(得分:0)
请参阅以下小提琴。
例如
var userName = $("input[name=userName]").val();
var subject = $("input[name=subject]").val();
var message = $("input[name=message]").val();
if (/^[a-zA-Z ]{2,30}$/.test(userName)) {
alert("Your name is in the correct format");
}
else{
alert("Your name can't contain numbers or other characters etc.");
return false;
}
答案 2 :(得分:0)
您可以按照此策略验证表单。
function submitObject(){
var object = {};
object.name = $(".name").val();
object.email = $(".email").val();
try{
if(this.validateForm(object )){
// call AJAX to save data.
}
}catch(error){
// show error message
}
}
function validateForm(object) {
if(this.isEmpty(object.name)){
throw "Parameter is not a null or empty!";
}
if(this.isEmpty(object.email)){
throw "Parameter is not a null or empty!";
}
return true;
}
function isEmpty(str) {
return (!str || 0 === str.length);
}