How to validate the Mobile number without using the Loop

Posted by Neeks under C# category on | Views : 8085
There is no need to write the Loop for validating the Digits of Mobile Number.
For testing the mobile Number the string.
You can achieve this by following code.

/*Here str is used to store the Mobile number's string*/
if (str.Length == 10)
double.Parse(str); // Here we are parsing the string as a double. If there is any invalid number like 9687rrt400, It will move to the exception part. And the error will be generated.
catch (Exception ex)
MessageBox.Show("Invalid cellNo");
MessageBox.Show("CellNo Should Contain exactly 10 Digits");

Comments or Responses

Posted by: Syedshakeer on: 2/12/2009 Level:Starter | Status: [Member]

Hi Neeks
you wrtie code only for how to check length of astring..
but you are not validating complete cellnumber.
for example i can enater cell number textbox as 924788wer5
then how you can validte it..
reply me

Posted by: Deeraj on: 5/15/2009 Level:Starter | Status: [Member]
Hi Neeks,

This tip is just to reduce development efforts.

Regular Expression - \d{10}

Use a RegularExpressionValidator control set the ControlToValidate property to the textbox where the Phone number will be keyed in.

NOTE: This will accept all 10 digits. Meaning, '0000000000' is also valid.



Login to post response