使用jquery联系表单验证错误

时间:2018-05-07 05:53:03

标签: javascript jquery html5 css3 validation

我正在通过为所有输入字段动态传递值来进行联系表单。我使用id i传递值在同一页面上有两个表单。在js中这两种形式的代码相同。问题是在传递正确的值之后它也显示错误,如果我提交form1,那么form2也会被提交。这里验证不起作用我试过一切都是正确的可能是什么问题。

$(document).ready(function() {

  /* contact form tabs*/
  function button() {
    $('.demos-nav .iva-nav').click(function() {
      var btn = $(this).data('btn');
      $('.row').removeClass('current').css('display', 'none');
      $('.demos-nav .iva-nav').removeClass('current');

      $(this).addClass('current');
      $("#" + btn).addClass('current').css('display', 'block');

    })
  }
  button();
  /* name*/
  $("[class^='form_']").on('input', function() {
    var input = $(this);
    var is_name = input.val();
    if (is_name) {
      input.removeClass("invalid").addClass("valid");
    } else {
      input.removeClass("valid").addClass("invalid");
    }
  });

  /* E-mail */
  $("[class^='form_']").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 */
  $("[class^='form_']").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");
    }
  });

  /* select People*/
  $("[class^='form_']").change(function() {
    var select = $(this);
    var selectOption = $("#contact_select option:selected").val();
    if (selectOption) {
      select.removeClass("invalid").addClass("valid");
    } else {
      select.removeClass("valid").addClass("invalid");
    }
  });

  /* select Time*/
  $("[class^='form_']").change(function() {
    var select = $(this);
    var selectTime = $("#contact_time option:selected").val();
    if (selectTime) {
      select.removeClass("invalid").addClass("valid");
    } else {
      select.removeClass("valid").addClass("invalid");
    }
  });

  /* Enquiry message*/
  $("[class^='form_']").keyup(function(event) {
    var input = $(this);
    var enquiry = $(this).val();
    if (enquiry) {
      input.removeClass("invalid").addClass("valid");
    } else {
      input.removeClass("valid").addClass("invalid");
    }
  });

  /* message*/
  $("[class^='form_']").keyup(function(event) {
    var input = $(this);
    var message = $(this).val();
    if (message) {
      input.removeClass("invalid").addClass("valid");
    } else {
      input.removeClass("valid").addClass("invalid");
    }
  });
  /* Submit */
  $("[id^='submit_']").click(function(event) {

    var form_data = $("[id^='form_']").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) {
      return false;
    } else {
      $('.success_msg').fadeIn().delay(3000).fadeOut();
      $('input , textarea , select').val('').removeClass('valid');
      event.preventDefault();
    }
  });
});
.wrapper {
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
  position: relative;
}

.demos-nav {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  margin: 40px auto;
  justify-content: center;
  align-items: center;
  max-width: 1140px;
}

.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: 50px;
  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);
  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 {
	transform: rotateX(-180deg) rotateY(180deg) rotateZ(180deg) ;
}*/

.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: 0;
  color: red;
}


/*input.invalid, 
textarea.invalid,
select.invalid {
	border: 2px solid 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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="demos-nav">
    <div class="iva-nav demo1" data-btn="demo1"><span>Demo 1</span></div>
    <div class="iva-nav demo2" data-btn="demo2"><span>Demo 2</span></div>

  </div>
  <!-- demos nav -->

  <div class="row current" id="demo1">
    <div class="cfar cfar-s1">

      <form id="form_info1" method="post" action="">
        <div class="detail">
          <input type="text" class="form_name1" id="contact_name1" name="name1" required autocomplete="off" />
          <span class="inputBar"></span>
          <!--inputBar-->
          <label for="contact_name1">Name</label>
          <span class="error">This field is required</span>
        </div>
        <!--detail-->
        <div class="detail">
          <input type="text" class="form_email1" id="contact_email1" name="email1" required autocomplete="off" />
          <span class="inputBar"></span>
          <!--inputBar-->

          <label for="contact_email1">Email</label>
          <span class="error">A valid email address is required</span>
        </div>
        <!--detail-->
        <div class="detail">
          <input type="number" class="form_phone1" id="contact_phone1" name="phone1" required autocomplete="off" />
          <span class="inputBar"></span>
          <!--inputBar-->
          <label for="contact_phone1">Phone Number</label>
          <span class="error">A valid Phone number is required</span>
        </div>
        <!--detail-->
        <div class="detail">
          <input type="text" class="form_enquiry1" id="contact_enquiry1" name="enquiry1" required autocomplete="off" />
          <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" class="form_message1" id="contact_message1" name="message1" required></textarea>
          <span class="inputBar"></span>
          <!--inputBar-->
          <label for="contact_message1">Type your Message here</label>
          <span class="error">This field is required</span>
        </div>
        <!--detail-->
        <div class="btn-container" id="submit_form1">
          <a href="#"><button type="submit" class="btn"> Submit</button></a>
        </div>

      </form>
      <div class="success_msg">
        <p>Form submitted Successfully</p>
      </div>

    </div>
    <!-- Contact Form style 1 -->
  </div>
  <!-- row  demo 1-->

  <div class="row" id="demo2">
    <div class="cfar cfar-s2">
      <div class="cfar-form">
        <h2 class="fancy-heading-s2"> Contact us </h2>
        <span class="fancy-title"> Reach out to us for any Inquiry</span>

        <form id="form_info2" method="post" action="">
          <div class="detail">
            <input type="text" class="form_name2" id="contact_name2" name="name2" required autocomplete="off" />
            <span class="inputBar"></span>
            <!--inputBar-->
            <label for="contact_name2">Name</label>
            <span class="error">This field is required</span>
          </div>
          <!--detail-->
          <div class="detail">
            <input type="text" class="form_email2" id="contact_email2" name="email2" required autocomplete="off" />
            <span class="inputBar"></s1pan><!--inputBar-->
									<label for="contact_email2">Email</label>
									<span class="error">A valid email address is required</span>
          </div>
          <!--detail-->
          <div class="detail">
            <input type="number" class="form_phone2" id="contact_phone2" name="phone2" required autocomplete="off" />
            <span class="inputBar"></span>
            <!--inputBar-->
            <label for="contact_phone2">Phone Number</label>
            <span class="error">A valid Phone number is required</span>
          </div>
          <!--detail-->
          <div class="detail message">
            <textarea type="text" class="form_message2" id="contact_message2" name="message2" required></textarea>
            <span class="inputBar"></span>
            <!--inputBar-->
            <label for="contact_message2">Type your Message here</label>
            <span class="error">This field is required</span>
          </div>
          <!--detail-->

          <div class="btn-container" id="submit_form2">
            <a href="#"><button type="submit" class="btn"> Submit</button></a>
          </div>
        </form>

      </div>
    </div>
    <!-- Contact Form style 2 -->
  </div>
  <!-- row  demo 2-->
</div>

3 个答案:

答案 0 :(得分:1)

在代码和设计中进行以下更改。 在html中,将按钮标记更改为输入标记

<div class="wrapper">
    <div class="demos-nav">
        <div class="iva-nav demo1" data-btn="demo1"><span>Demo 1</span></div>
        <div class="iva-nav demo2" data-btn="demo2"><span>Demo 2</span></div>

    </div>
    <!-- demos nav -->

    <div class="row current" id="demo1">
        <div class="cfar cfar-s1">

            <form id="form_info1" method="post" action="">
                <div class="detail">
                    <input type="text" class="form_name1" id="contact_name1" name="name1" required autocomplete="off" />
                    <span class="inputBar"></span>
                    <!--inputBar-->
                    <label for="contact_name1">Name</label>
                    <span class="error">This field is required</span>
                </div>
                <!--detail-->
                <div class="detail">
                    <input type="text" class="form_email1" id="contact_email1" name="email1" required autocomplete="off" />
                    <span class="inputBar"></span>
                    <!--inputBar-->

                    <label for="contact_email1">Email</label>
                    <span class="error">A valid email address is required</span>
                </div>
                <!--detail-->
                <div class="detail">
                    <input type="text" class="form_phone1" id="contact_phone1" name="phone1" required autocomplete="off" />
                    <span class="inputBar"></span>
                    <!--inputBar-->
                    <label for="contact_phone1">Phone Number</label>
                    <span class="error">A valid Phone number is required</span>
                </div>
                <!--detail-->
                <div class="detail">
                    <input type="text" class="form_enquiry1" id="contact_enquiry1" name="enquiry1" required autocomplete="off" />
                    <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" class="form_message1" id="contact_message1" name="message1" required></textarea>
                    <span class="inputBar"></span>
                    <!--inputBar-->
                    <label for="contact_message1">Type your Message here</label>
                    <span class="error">This field is required</span>
                </div>
                <!--detail-->
                <div class="btn-container" id="submit_form1">
                    <a href="#"><input type="button" class="btn" value="Submit"/></a>
                </div>

            </form>
            <div class="success_msg">
                <p>Form submitted Successfully</p>
            </div>

        </div>
        <!-- Contact Form style 1 -->
    </div>
    <!-- row  demo 1-->

    <div class="row" id="demo2">
        <div class="cfar cfar-s2">
            <div class="cfar-form">
                <h2 class="fancy-heading-s2"> Contact us </h2>
                <span class="fancy-title"> Reach out to us for any Inquiry</span>

                <form id="form_info2" method="post" action="">
                    <div class="detail">
                        <input type="text" class="form_name2" id="contact_name2" name="name2" required autocomplete="off" />
                        <span class="inputBar"></span>
                        <!--inputBar-->
                        <label for="contact_name2">Name</label>
                        <span class="error">This field is required</span>
                    </div>
                    <!--detail-->
                    <div class="detail">
                        <input type="text" class="form_email2" id="contact_email2" name="email2" required autocomplete="off" />
                        <span class="inputBar">
                            </s1pan><!--inputBar-->
                            <label for="contact_email2">Email</label>
                            <span class="error">A valid email address is required</span>
                    </div>
                    <!--detail-->
                    <div class="detail">
                        <input type="text" class="form_phone2" id="contact_phone2" name="phone2" required autocomplete="off" />
                        <span class="inputBar"></span>
                        <!--inputBar-->
                        <label for="contact_phone2">Phone Number</label>
                        <span class="error">A valid Phone number is required</span>
                    </div>
                    <!--detail-->
                    <div class="detail message">
                        <textarea type="text" class="form_message2" id="contact_message2" name="message2" required></textarea>
                        <span class="inputBar"></span>
                        <!--inputBar-->
                        <label for="contact_message2">Type your Message here</label>
                        <span class="error">This field is required</span>
                    </div>
                    <!--detail-->

                    <div class="btn-container" id="submit_form2">
                        <a href="#"><input type="button" class="btn" value="Submit"/></a>
                    </div>
                </form>

            </div>
        </div>
        <!-- Contact Form style 2 -->
    </div>
    <!-- row  demo 2-->
</div>

脚本是,

  $(document).ready(function () {

        /* contact form tabs*/
        function button() {
            $('.demos-nav .iva-nav').click(function () {
                var btn = $(this).data('btn');
                $('.row').removeClass('current').css('display', 'none');
                $('.demos-nav .iva-nav').removeClass('current');

                $(this).addClass('current');
                $("#" + btn).addClass('current').css('display', 'block');

            })
        }
        button();
        /* name*/
        $("[class^='form_name']").on('change', function () {
            var input = $(this);
            var is_name = input.val();
            if (is_name) {
                $(input).removeClass("invalid");
                $(input).addClass("valid");
            } else {
                $(input).removeClass("valid");
                $(input).addClass("invalid");
            }
        });

        /* E-mail */
        $("[class^='form_email']").on('change', 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 */
        $("[class^='form_phone']").on('change', 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");
            }
        });

        /* Enquiry message*/
        $("[class^='form_enquiry']").keyup(function (event) {
            var input = $(this);
            var enquiry = $(this).val();
            if (enquiry) {
                input.removeClass("invalid").addClass("valid");
            } else {
                input.removeClass("valid").addClass("invalid");
            }
        });

        /* message*/
        $("[class^='form_message']").keyup(function (event) {
            var input = $(this);
            var message = $(this).val();
            if (message) {
                input.removeClass("invalid").addClass("valid");
            } else {
                input.removeClass("valid").addClass("invalid");
            }
        });
        /* Submit */
        $("[id^='submit_']").click(function (event) {

            var form_data = $(this).parent().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) {
                return false;
            } else {
                $('.success_msg').fadeIn().delay(3000).fadeOut();
                $('input , textarea , select').val('').removeClass('valid');
                event.preventDefault();
            }
        });
    });

答案 1 :(得分:0)

在提交处理程序中,您正在引用这两个表单。这就是两种形式的验证都被解雇的原因。尝试获取单击的提交按钮所属的表单的引用。

$("[id^='submit_']").click(function(event) {
   var form = $(this).closest("form");
   var form_data = form.serializeArray();
    //remaining code

希望这会给你一个想法。

答案 2 :(得分:-1)

您是否尝试使用最新版本的jQuery?喜欢:3.3.1

  

更新

尝试更改所有if语句,例如:

crash

codepen