问题描述
在这里,我有一个 ASP:文本框,一个 RegularEx pressionValidator 和 ValidatorCalloutExtender
Here I have an asp:TextBox, a RegularExpressionValidator and a ValidatorCalloutExtender.
我的问题是:没有onkeyup事件,它工作正常,文本框进行验证时,它的无效变成红色结果。
但是,当我添加事件,它停止验证,文本框可以有任何的文字里面,它不会变成红色。结果
下面是一些code:
My issue is: without the OnKeyUp event, it works fine, the TextBox is validated and becomes red when it's invalid.
But when I add the event, it stops validating, the textbox can have any text inside, and it will not become red.
Here's some code:
ASP.NET:
<asp:TextBox
ID="txtTelefone"
runat="server"
OnKeyUp="maskField(this, event)"
Text="()-"
MaxLength="14">
</asp:TextBox>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator_Telefone"
runat="server"
ControlToValidate="txtTelefone"
ValidationExpression="(^\(\d{2}\)\d{4,5}-\d{4}$)|(^\(\)\-$)"
Display="None"
ErrorMessage="Invalid format!<br>Phone: (xx)xxxx-xxxx<br>Cel: (xx)xxxxx-xxxx">
</asp:RegularExpressionValidator>
<ajaxToolkit:ValidatorCalloutExtender
ID="ValidatorCalloutExtender_Telefone"
runat="server"
TargetControlID="RegularExpressionValidator_Telefone"
HighlightCssClass="txt-TextBox-Error"
WarningIconImageUrl="~/Image/BackGroundAjax.png"
PopupPosition="left">
</ajaxToolkit:ValidatorCalloutExtender>
JavaScript的:
function maskField(textbox, event) {
//MASK TO PHONE NUMBERS -> FORMAT (99)99999-9999 OR (99)9999-9999
if (event.char.match(/[0-9()-]/) || event.which == 8 || event.which == 46) {
var next = textbox.selectionStart;
var text = textbox.value.replace(/\D/g, "");
var ddd = text.substring(0, 2).replace(/\ /, "");
var firstSet = text.length == 11 ? text.substring(2, 7) : text.substring(2, 6);
var secondSet = text.length == 11 ? text.substring(7, 11) : text.substring(6, 10);
textbox.value = "(" + ddd + ")" + firstSet + "-" + secondSet;
if (textbox.value.charAt(next - 1).match(/\D/) && event.char.match(/[0-9()-]/))
next++;
textbox.selectionStart = next;
textbox.selectionEnd = next;
textbox.focus();
}
}
编辑:收到我的第一个答案后更新:结果
对不起,但我需要的onkeyup事件,因为我需要找出导致该事件的关键。
Update after receiving my first answer:
Sorry, but I need the OnKeyUp event because I need to identify the key that caused the event.
推荐答案
我觉得这不应该是替代的onkeyup OnTextChanged。你可以把它参考这个网页。
I think it should not be OnKeyUp instead OnTextChanged. You can refer it to this page onkeyup event asp.net
这篇关于RegularEx pressionValidator不使用JavaScript onkeyup事件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!