Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

How do I auto insert dashes in a phone number?

I have a request to "automatically add dashes" to an input field designated to hold a phone number. How do I accomplish this in C# and / or the markup? Should I try to use a regular expression validator?
Avatar of waheebc
waheebc

You can use multiple test boxes and then while concatenating add a hifen
what I meant was 'text' boxes
Avatar of Michael Sterling

ASKER

i can give that a shot,...is there something wrong with the code i've attached? the OnTextChanged event never seems to fire?
<li>
                            <asp:Label runat="server" Text="Phone number:" AssociatedControlID="txtPhone" />
                            <asp:TextBox ID="txtPhone" runat="server" Width="100" MaxLength="12" FieldName="Phone" OnTextChanged="txPhone_TextChanged"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="txtPhone" Display="Dynamic"
                                ErrorMessage="Please enter your phone number">*</asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ID="regPhone" ControlToValidate="txtPhone" Display="Dynamic"
                                runat="server" ValidationExpression="\d{3}\d{3}\d{4}"> Phone number must be in the format of 9999999999.
                            </asp:RegularExpressionValidator>
                        </li>


    public void txPhone_TextChanged(object sender, EventArgs e)
    {
        if ( (txtPhone.ToString().Length == 3) || (txtPhone.ToString().Length == 7))
            txtPhone.Text = txtPhone.Text.ToString() + "-";
    }

Open in new window

try txtPhone.text.ToString() instead.
yeh i tried that and it's not working as i expect it to. it never fires the event. i'm expecting that every time the contents are changed this event should fire but it never does????
public void txPhone_TextChanged(object sender, EventArgs e)
    {
        if ((txtPhone.Text.ToString().Length == 3) || (txtPhone.Text.ToString().Length == 7))
            txtPhone.Text = txtPhone.Text.ToString() + "-";
    }

Open in new window

when i set the AutoPostBack property of the textbox to true. it only checks once the user leaves the checkbox.
you should not use codebehind for such task
use javascript onchange  function
Avatar of Daniel Van Der Werken
It will never fire no matter what.  You really don't want to do it that way anyway.  Try this:
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head runat="server">
      <title>CK Editor Example</title>
      <script type="text/javascript">
          function setHyphens() {
              var pt = document.getElementById("_prvPhone");
              var s = "0";
              s = pt.value;
              if ( s.length > 1 && ( s.length == 3 || s.length == 7 ) ) {
                  pt.value += '-';
              }
          }
      </script>
   </head>
   <body>
      <form>
         <div>
            <input type="text" id="_prvPhone" onkeydown="setHyphens();" />
         </div>
      </form>
   </body>
</html>
@fryezz, oh that sounds like a good idea. any examples?
Well, it will fire when you hit the enter key, but it won't fire on text change like you expect.
@Dan7el: yep...
@Dan7el: I'm looking for something that will fire each time the text is changed in the text box, while i'm still in the text box. not after i leave it....
check this url for elegent formating, or customize it to have only dashes

http://www.kodyaz.com/articles/javascript-phone-format-phone-number-format.aspx
ASKER CERTIFIED SOLUTION
Avatar of Michael Sterling
Michael Sterling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks again.