3

I want to apply validation to a text box which stores a 10-digit phone number

How can I validate phone numbers in text boxes in Windows Phone 7?

Kara
  • 6,115
  • 16
  • 50
  • 57
sona
  • 71
  • 4
  • 7

5 Answers5

3

If you want use 10 number mobile validation than set in your MaxLength="10" and for phone number only InputScope="TelephoneNumber" this all in XAML side like

<TextBox   Name="txtPhoneNo" Width="310"  Text=""  InputScope="TelephoneNumber" MaxLength="10" KeyDown="txtContactUsPhone_KeyDown" />

Now genrate keydown event of your Textbox and pest bellow code..

private void txtContactUsPhone_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.Key.ToString(), "[0-9]"))
                e.Handled = false;
            else e.Handled = true;
        }

Hope it will help you

Thanks

Mansinh
  • 1,365
  • 4
  • 19
  • 47
0

you can set the Textbox.Maxlength property, and set the input scope to Numbers only.

thank you.

Noorul
  • 873
  • 2
  • 9
  • 26
0
using System.Text.RegularExpressions;public static bool IsItNumber(string inputvalue){Regex isnumber = new Regex("[^0-9]");return !isnumber.IsMatch(inputvalue);}

or

public bool Main(string text)

{

  if ( !Regex.Match(text,@"^[1-9]\d{2}-[1-9]\d{2}-\d{4}$" ).Success )
  {
     return false
  }   else {
return true
}
   }
PandaSharp
  • 642
  • 7
  • 24
0

ill refer you to the following link, the question regards how to validate a phone number using a regex, in your case one way would be to use the regex within the setter of the property you are binding for the phone number and throw a ValidationException if it fails, when binding include the ValidatesOnException option

like

 public string PhoneNumber
 {
    get { return _phoneNumber; }
    set { 
       if (!IsValidPhoneNumber(value))
             throw new ValidationException("Invalid Phone Number");
       _phoneNumber = value;
     }
 }

with a binding expression something like

{Binding PhoneNumber, ValidatesOnException=True}
Community
  • 1
  • 1
almog.ori
  • 7,839
  • 1
  • 35
  • 49
  • How does for example a TextBox react if its binding throws this exception. Will it be red or something? – Rico Suter Sep 07 '11 at 11:31
  • Validate phone number which is user input by exception is a bad practice. It's kind of vexing exceptions - no really exceptional situation. I know it is a part of XAML, but I don't like it. I'd rather use bool var. – Lukasz Madon Sep 07 '11 at 11:33
  • @rico yes the control template has built in styling that will show a red border and tooltip with the exception msg. – almog.ori Sep 07 '11 at 11:40
  • 1
    I believe there's no ValidationException in WP7 (at least not in Mango RC). You'll have to throw your own ones _and_ handle the BindingValidationError event. – ehnmark Sep 07 '11 at 12:02
  • 1
    @ehnmark you could very well be right, i just used it as an example, but any exception can be thrown... – almog.ori Sep 07 '11 at 12:04
-1

This is gonna be simple. The easy method is to do is, add a eventhandler called "PreviewTextInput" in the textbox.

<TextBox x:Name="txtbox_PhoneNum" PreviewTextInput="txtbox_PhoneNum_PreviewTextInput"/>

In the .cs file,

private void txtbox_PhoneNum_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        Regex reg = new Regex("[^0-9]+");
        e.Handled = reg.IsMatch(e.Text);
    }

If the e.Handled is true, you are entering alpha characters and finally you can do filtration by this.

Shafiq Abbas
  • 484
  • 1
  • 5
  • 18