否则如果条件在jquery中不起作用

时间:2015-03-10 12:58:30

标签: jquery asp.net

我使用注册表单作为弹出窗口。所以我使用jquery验证表单。如果条件不起作用,那就不行了。我不知道什么是问题????

脚本的编码:

$(document).on('click', "#btnregister", function (e) {
            e.preventDefault();

            var name = $("#name").val();
            var formemail = $("#email").val();
            var formreemail = $("#confirmemail").val();
            var email = $("#email").val();
            var confirmemail = $("#confirmemail").val();
            var emailRegex = "[A-Z0-9._%+-]+@@[A-Z0-9.-]+\.[A-Z]{2,6}$";

            if ($("#name").val() == "") {
                    $("#name").focus();
                    $("#errorBox").html("Please fill YourName...");
            } else if ($("#email").val() == "") {
                    $("#email").focus();
                    $("#errorBox").html("Please fill YourEmail...");

            } else if ($(name != '' && email != '' && confirmemail != '')) {
                $("#errorBox").html("...ThankYou for Your Registration!..")
            }
            var url = $(this).attr('data-url');

            $.ajax({
                type: 'POST',
                url: url,
                data: $('#formRegister').serialize(),
                success: function (data) {
                    $('#widgetContainer1').html(data);
                    $('#widgetModal1').modal('show');
                    $.validator.unobtrusive.parse('#widgetContainer1');

                }


            });

        });

1 个答案:

答案 0 :(得分:0)

  1. 你的正则表达式是错误的 - 它需要/ /而不是引用,如在var emailRegex = /[A-Z0-9._%+-]+@@[A-Z0-9.-]+\.[A-Z]{2,6}$/;

  2. 如果有错误你不需要ajax而不要感谢他们直到成功

  3. 我会抓住提交事件而不是点击按钮

  4. $(function() {
      $("#regform").on("submit", function(e) {
        e.preventDefault();
    
        var name = $("#name").val();
        var formemail = $("#email").val();
        var formreemail = $("#confirmemail").val();
        var email = $("#email").val();
        var confirmemail = $("#confirmemail").val();
        var emailRegex = /[A-Z0-9._%+-]+@@[A-Z0-9.-]+\.[A-Z]{2,6}$/;
        if ($("#name").val() == "") {
          $("#name").focus();
          $("#errorBox").html("Please fill your name...<br/>");
        } else if ($("#email").val() == "") {
          $("#email").focus();
          $("#errorBox").html("Please fill your email...<br/>");
        } else if (!emailRegex.test(formemail)) {
          $("#email").focus();
          $("#errorBox").html("Please fill a valid email...<br/>");
        } else if ($('#confirmemail').val() == "") {
          $("#confirmemail").focus();
          $("#errorBox").html("Please confirm your email...<br/>");
        } else if (!emailRegex.test(formreemail)) {
          $("#confirmemail").focus();
          $("#errorBox").html("Please fill the valid email in confirm field... ");
        } else if (confirmemail != email) {
          $("#confirmemail").focus();
          $("#errorBox").html("Emails are not matching, Please re-enter again...<br/>");
        } else if ($(name != '' && email != '' && confirmemail != '')) {
          var url = $(this).attr('data-url');
          $.ajax({
            type: 'POST',
            url: url,
            data: $('#formRegister').serialize(),
            success: function(data) {
              $('#widgetContainer1').html(data);
              $('#widgetModal1').modal('show');
              $.validator.unobtrusive.parse('#widgetContainer1');
              $("#errorBox").html("...Thank you for your registration!..<br/>")
            }
          });
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <form id="regform">
      Name:
      <input id="name" value="" />
      <br/>Email:
      <input id="email" value="" />
      <br/>Confirm:
      <input id="confirmemail" value="" />
      <br/>
      <input type="submit" />
      <div id="errorBox"></div>