1

How do you make a textbox only accept 10 numbers and it could also accept () -

example: 123-123-1234 or example: 1231231234 or example: (123)123-1234

if the text box does not contain any of these example it should give a error message.

2 Answers2

1

The MaskedTextBox in Windows Forms is designed to restrict input to conform to a pattern and gracefully let the user know if they're entering data that's against that pattern.

https://msdn.microsoft.com/en-us/library/vstudio/kkx4h3az(v=vs.110).aspx

Rikki Gibson
  • 4,136
  • 23
  • 34
  • The masked textbox is a good tool but i dont know how to make the mask text box give me an error if the user doesn't have 10 numbers in it. –  Jul 19 '15 at 06:24
  • You need to handle the validation failed event as demonstrated in the article. – Rikki Gibson Jul 19 '15 at 06:38
  • This article is great for the DateTime, but it does not explain how to validate a phone number –  Jul 19 '15 at 06:45
  • Look in the ... menu by Mask in the Properties window of the designer. If that doesn't have a premade mask for phone numbers, you can invent one by experimenting with the example pattern used for dates in the tutorial-- just, instead of "00/00/0000" try "000-000-0000" or something. – Rikki Gibson Jul 19 '15 at 06:58
  • I can got the masked textbox in my form. I can input numbers in it like "000-000-0000" but the thing is when i click a button, and if a user missed a number in the masked textbox. User types this: "000-000-000_"<--They forgot a number. How do you program the masked textbox to show you can error, by the click of the button. –  Jul 19 '15 at 07:06
  • Thanks for taking your time to help me out, but i figured it out. –  Jul 19 '15 at 07:24
0

You have to do this with server-code, but it is nice to have a client-side validator as well.

Server-side: As Rikkigibson already pointed out, MaskedTextBox is a good control to achieve this. Example:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.ToolTip1.IsBalloon = True
    Me.MaskedTextBox1.Mask = "00/00/0000"
End Sub

Private Sub MaskedTextBox1_MaskInputRejected(sender as Object, e as MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
    ToolTip1.ToolTipTitle = "Invalid Input"
    ToolTip1.Show("We're sorry, but only digits (0-9) are allowed in dates.", MaskedTextBox1, 5000)
End Sub

Read here about the Mask property.

Client-side: It is good to have a client-side validation as well, so, no request with wrong value in the textbox would reach and burden your server. To do this in jQuery, you can handle the .keyup() event of your textbox. So, if you have a validation function, and we assume that selector corresponds to your textbox, then you should write a code, like

$(selector).keyup(function(e) {
    if (validation(e)) {
        //it is valid
    } else {
        //it is not valid
    }
});

EDIT:

I have seen you have a problem with the number of digits as well. This is how you get the number of digits in a String using vb and this is how you get it in Javascript.

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • If i approached this issue with the masked textbox for the phone number the user can miss a number example: (123) 123-123_ and still be able to bypass the program with no warnings. is there a way to prompt the user an error message with the masked textbox indicating that they dont have enough numbers in the maskedtextbox @Lajos Arpad –  Jul 19 '15 at 06:35
  • The comment on the original post mentions that this Winforms, not web, so there's no VB vs JavaScript concern here. – Rikki Gibson Jul 19 '15 at 06:38
  • @rikkigibson, you are right, we can forget about the Javascript part. – Lajos Arpad Jul 19 '15 at 06:44
  • @LajosArpad , yes I'm using the phone number mask. This is for validating phone numbers. –  Jul 19 '15 at 06:50
  • Thanks for taking your time to help me out, but i figured it out –  Jul 19 '15 at 07:24