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?
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?
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
you can set the Textbox.Maxlength property, and set the input scope to Numbers only.
thank you.
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
}
}
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}
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.