Reformat TextBox value prior validation

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

intrader

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);
}
Is the server side validator giving the message?
 
O

Oleg Ogurok

No, once I've set EnableClientScript="False" on the validator and clicked a
button, the server-side validation said OK.

-Oleg.

intrader said:
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);
}
Is the server side validator giving the message?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top