How to validate a Cell Number in Windows foms

Syedshakeer
Posted by in C# category on for Beginner level | Views : 16232 red flag

How to validate a Cell Number in Windows foms



Validating a cellnumber textbox in Windows Form usinr c#

 

First Add a textbox and button on windows form

 

Keep name of textbox as textcellno

Double click the button and write below code



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
String str=Convert.ToString(textcellno.Text);

if(str .Length ==10)
{


String strcell=String.Empty;
for(int i=0;i<10;i ++)
{
if(Char.IsDigit(str[i]))
{

}
else
{
MessageBox.Show("Invalid cellNo");
break;
}
}

MessageBox.Show("you cell Number is valid:" + str);
}
else
{
MessageBox.Show("CellNo Should Contain exactly 10 Digits");
}

}
}
}

Page copy protected against web site content infringement by Copyscape

About the Author

Syedshakeer
Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
http://www.dotnetfunda.com
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Posted by: Neeks on: 2/8/2009
There is no need to write the Loop for validating the Digits for testing the string the string.
You can achieve this by following code.

if (str.Length == 10)

{
try
{
double.Parse(str);
}
catch (Exception ex)
{
MessageBox.Show("Invalid cellNo");
}
}
else
{
MessageBox.Show("CellNo Should Contain exactly 10 Digits");
}


Login to post response

Comment using Facebook(Author doesn't get notification)