I need some help on formating a textbox, I would like to do the following, if a user enters 9998887777 to on the leave event to format the number to this (999) 888-7777,
Thanks
I need some help on formating a textbox, I would like to do the following, if a user enters 9998887777 to on the leave event to format the number to this (999) 888-7777,
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
JUliusPosted Jul 17, 2008, 12:02 PM
Thank you so much Alan.
Julius
Thulani MathenjwaPosted Mar 24, 2009, 6:33 AM
{
string d = string.Format("({0}) {1}-{2}", phoneN.Substring(0, 3), phoneN.Substring(3, 3), phoneN.Substring(6));
return d;
}
JUliusPosted Jul 17, 2008, 4:38 PM
Satish KathiPosted Jul 17, 2008, 3:48 PM
Set the "TextMaskFormat" propety of the Masked Text Box to "ExcludePromptAndLiterals"
JUliusPosted Jul 17, 2008, 3:32 PM
Hello Satish:
I have :-), the problem that I had is that the pnone number is a required field when saving the record, and I have added the following to the save event:
If TxtPhone.Text = "" Then
Msgbox "The Phone number is required"
Exit Sub
End if
When I am testing to see if would allow to save , it does save the record, because the Masked of the field is ( ) __-___, it thiks that is something in the field already.
Satish KathiPosted Jul 17, 2008, 12:25 PM
AlanPosted Jul 17, 2008, 11:58 AM
No problem, Julius:
Private
Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave Dim num As ULong If TextBox1.Text.Length = 10 AndAlso ULong.TryParse(TextBox1.Text, num) Then Dim pn As String = TextBox1.TextTextBox1.Text =
String.Format("({0}) {1}-{2}", pn.Substring(0, 3), pn.Substring(3, 3), pn.Substring(6)) ElseMessageBox.Show(
"Invalid phone number, please change")TextBox1.Focus()
End If End SubJUliusPosted Jul 17, 2008, 11:50 AM
I am sorry Alan, my mistake; I am working in VB2005, Thanks,
Julius
AlanPosted Jul 17, 2008, 11:28 AM
This should do it:
private
void textBox1_Leave(object sender, EventArgs e){
ulong num; if (textBox1.Text.Length == 10 && ulong.TryParse(textBox1.Text, out num)){
string pn = textBox1.Text;textBox1.Text =
String.Format("({0}) {1}-{2}", pn.Substring(0,3), pn.Substring(3,3), pn.Substring(6));}
else{
MessageBox.Show("Invalid phone number, please change");textBox1.Focus();
}
}