[英]ASP.NET Disable Button After Validating Regular Expression of Textbox
I want to disable the asp button after the user submit the form with the correct regular expression 我想在用户使用正确的正则表达式提交表单后禁用asp按钮
Here is my form 这是我的表格
<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>
this is the validation javascript function 这是验证javascript函数
<script>
function myFunction2() {
if (Page_ClientValidate('S')) {
document.getElementById('Button1').disabled = true;
document.getElementById('Button1').value = "Please waitً";
}
}
</script>
The problem is the button is disabled after the text box has any value which did not pass the email regex check 问题是文本框具有未通过电子邮件正则表达式检查的任何值后,按钮被禁用
Code Behind 背后的代码
protected void Button1_Clicked(object sender, EventArgs e) { System.Threading.Thread.Sleep(5000); }
It suppose to you wanna lock submit button during server side processing. 它假定您想在服务器端处理期间锁定提交按钮。 For doesing the trick, use
OnClientClick
attribute of asp:Button
in below approach: 为此,请使用以下方法使用
asp:Button
OnClientClick
属性:
<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 ...';" />
So the button will be disabled when user click on it, and it will be enabled when the server side operation will be finished. 因此,当用户单击该按钮时,该按钮将被禁用,而在服务器端操作完成时,该按钮将被启用。
How about some client-side JavaScript magic: 客户端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>
However, client-side validation is simply not enough to be safe. 但是,客户端验证根本不够安全。 All sorts of things can happen.
各种各样的事情都可能发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.