O
Oleg Ogurok
Hi there,
I have a modified ASP.NET TextBox control that formats its output as the
phone number, e.g. if you enter "1234567890" it'll change the value to
"123-456-7890". This is done by a javascript function called on "onblur"
event of the textbox.
However, it seems validation happens before the onblur event, therefore if I
enter "1234567890", the validator that checks the phone number is set to
"Invalid" state. How can I format the data in TextBox prior to validation?
Thanks.
<xxx:TelephoneTextBox id="tb" runat="server"></xxx:TelephoneTextBox>
<asp:RegularExpressionValidator id="revCell" runat="server"
ErrorMessage="Cell phone number is invalid" ControlToValidate="tb"
Display="Dynamic" ValidationExpression="((\(\d{3}\)
?)|(\d{3}-))?\d{3}-\d{4}">[Invalid]</asp:RegularExpressionValidator>
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter
writer)
{
base.AddAttributesToRender (writer);
writer.AddAttribute("onblur", "TelephoneTextBox_Format(this)");
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
string scriptFormat =
"<script type=\"text/javascript\">\n" +
" function TelephoneTextBox_Format(source)\n" +
" {\n" +
" var phone = source.value\n" +
" phone = phone.replace(/\\s/g, \"\");\n" +
" if (phone.match(/[^\\d]/))\n" +
" return;\n" +
" if (phone.length != 10)\n" +
" return;\n" +
" source.value = phone.substr(0, 3) + '-' + phone.substr(3, 3) + '-' +
phone.substr(6, 4);\n" +
" }\n" +
"</script>";
Page.RegisterClientScriptBlock("TelephoneTextBox_Format", scriptFormat);
}
I have a modified ASP.NET TextBox control that formats its output as the
phone number, e.g. if you enter "1234567890" it'll change the value to
"123-456-7890". This is done by a javascript function called on "onblur"
event of the textbox.
However, it seems validation happens before the onblur event, therefore if I
enter "1234567890", the validator that checks the phone number is set to
"Invalid" state. How can I format the data in TextBox prior to validation?
Thanks.
<xxx:TelephoneTextBox id="tb" runat="server"></xxx:TelephoneTextBox>
<asp:RegularExpressionValidator id="revCell" runat="server"
ErrorMessage="Cell phone number is invalid" ControlToValidate="tb"
Display="Dynamic" ValidationExpression="((\(\d{3}\)
?)|(\d{3}-))?\d{3}-\d{4}">[Invalid]</asp:RegularExpressionValidator>
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter
writer)
{
base.AddAttributesToRender (writer);
writer.AddAttribute("onblur", "TelephoneTextBox_Format(this)");
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
string scriptFormat =
"<script type=\"text/javascript\">\n" +
" function TelephoneTextBox_Format(source)\n" +
" {\n" +
" var phone = source.value\n" +
" phone = phone.replace(/\\s/g, \"\");\n" +
" if (phone.match(/[^\\d]/))\n" +
" return;\n" +
" if (phone.length != 10)\n" +
" return;\n" +
" source.value = phone.substr(0, 3) + '-' + phone.substr(3, 3) + '-' +
phone.substr(6, 4);\n" +
" }\n" +
"</script>";
Page.RegisterClientScriptBlock("TelephoneTextBox_Format", scriptFormat);
}