Michael Sterling
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?
You can use multiple test boxes and then while concatenating add a hifen
what I meant was 'text' boxes
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() + "-";
}
try txtPhone.text.ToString() instead.
ASKER
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() + "-";
}
ASKER
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
use javascript onchange function
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>
<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("_
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>
ASKER
@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.
ASKER
@Dan7el: yep...
ASKER
@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
http://www.kodyaz.com/articles/javascript-phone-format-phone-number-format.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks again.