代码之家  ›  专栏  ›  技术社区  ›  Sherif El Sherif

ASP。验证Textbox正则表达式后的NET Disable按钮

  •  1
  • Sherif El Sherif  · 技术社区  · 7 年前

    我想在用户使用正确的正则表达式提交表单后禁用asp按钮

    这是我的表格

    <asp:TextBox ID="TextBox2" runat="server" placeholder="Email" class="form-control" type="email" Font-Size="Large" oninvalid="setCustomValidity(')"></asp:TextBox>
    
    <asp:Button ID="Button1" runat="server" class="btn btn-primary btn-block btn-lg" Text="Submit" OnClick="Button1_Click" ValidationGroup="S" UseSubmitBehavior="false" OnClientClick="myFunction2()" />
    
    <asp:RegularExpressionValidator ID="RegularExpressionValidator3" ControlToValidate="TextBox2" runat="server" ErrorMessage="Invalid Email" ValidationGroup="S" Visible="false" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
    

    这是验证javascript函数

    <script>
        function myFunction2() {
            if (Page_ClientValidate('S')) {
                                        document.getElementById('Button1').disabled = true;
                        document.getElementById('Button1').value = "Please waitً";
             }
     } 
    </script>
    

    问题是,在文本框具有任何未通过电子邮件regex检查的值后,该按钮被禁用

    代码隐藏

    protected void Button1_Clicked(object sender, EventArgs e) { System.Threading.Thread.Sleep(5000); }

    2 回复  |  直到 7 年前
        1
  •  0
  •   wp78de    7 年前

    一些客户端JavaScript魔术怎么样:

    var goodEmail = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    var goodEntry = /^^(?:(?:[^<>()[\]\\.,;:\s@\"]*(?:\.[^<>()[\]\\.,;:\s@\"]*)*)|(?:\"?.*\"?))@?(?:(?:\[?[0-9]{0,3}\.?[0-9]{0,3}\.?[0-9]{0,3}\.?[0-9]{0,3}\]?)|(?:(?:[a-zA-Z\-0-9]*\.)*[a-zA-Z]{0,}))$/;
    
    $("input")
      .data("oldValue", "")
      .bind("input propertychange", function() {
    var $this = $(this);
    var newValue = $this.val();
    
    if (!goodEntry.test(newValue))
        return $this.val($this.data("oldValue"));
    if (goodEmail .test(newValue)) {
        $this.removeClass("redborder");
        $("#submitBtn").prop("disabled",false);
    }
    else {
        $this.addClass("redborder");
        $("#submitBtn").prop("disabled",true);
    }
    
    return $this.data("oldValue", newValue);
      });
    
    function submitit() {
        console.log($("#email").val() + " is valid: Ready to proceed!");
    }
    .redborder { border: 1px solid red; }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <form>
      <p>Enter an email address:</p>
      <input id='email'>
      <button type='submit' disabled id='submitBtn' onclick="submitit()">Submit</button>
    </form>

    然而,客户端验证仅仅是不够安全的。各种事情都可能发生。

        2
  •  0
  •   Pezhman Parsaee    7 年前

    它假设您想在服务器端处理期间锁定提交按钮。如果要耍花招,请使用 OnClientClick 的属性 asp:Button 在以下方法中:

    <asp:Button 
      ID="Button1" 
      runat="server" 
      class="btn btn-primary btn-block btn-lg" 
      Text="Submit" 
      OnClick="Button1_Click" 
      UseSubmitBehavior="false" 
      OnClientClick="this.disabled = true; this.value = 'Processing ...';" />
    

    因此,当用户单击该按钮时,该按钮将被禁用,当服务器端操作完成时,该按钮将被启用。