Hi in my country we can phone anyone by only 7 number... Eg 2345897
and sometimes we represent it in this format 234-5897 by a - (negative)..

i want to tell the txtPhoneNumber that after 3 numbers ahve been entered a negative should be place and it should comes automatically ...
i do not want any user to press on the negative frm the keyboard..

I need help 4 this ..

Recommended Answers

All 4 Replies

Hi.
u can use masked text box for ur requirement

You can use regular expression to validate user input, in your case your regular expression'd be ^\d{3}-\d{4}$
So, on your btn validate click event handler you can check if user enters valid phoen number or not

if Regex.IsMatch(txtPhoneNumber.Text, textBox1.Text)
then
MessageBox.Show("Correct")
else
MessageBox.Show("Wrong")

This code if u want to add - after 3 numbers :

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Length = 3 Then
            TextBox1.Text = TextBox1.Text + "-"
        End If
    End Sub

This Following code if u want - (negative) will added after user input 7 numbers.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Length = 7 Then
            TextBox1.Text = Microsoft.VisualBasic.Left(TextBox1.Text, 3) + "-" + Microsoft.VisualBasic.Mid(TextBox1.Text, 4, TextBox1.Text.Length)
        End If
    End Sub
commented: awesome +1

But this issue can be solved by using masked text box.
if we use no need to write code also

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.