Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Codes Author
Wed, 20-Aug-2014 Authors
All Time Authors
Satyapriyanayak
12160
Jayakumars
8120
Niladri.biswas
6000

Latest members | More ...


(Statistics delayed by 5 minutes)
 Codes

textbox validation in windows application using c#...

Gopal_nivas
Posted by Gopal_nivas under Windows Forms on 10/14/2009 5:14:40 AM | Views : 70147 | Status : [Member]

windows application
+++++++++++++++++++++++++++++++

textbox validation
+++++++++++++++++++++++++++++++


mail id validation
|||||||||||||||||||||||||||

private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if(!ValidEmailAddress(textBox1.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textBox1.Select(0, textBox1.Text.Length);

// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textBox1, errorMsg);
}
}

private void textBox1_Validated(object sender, System.EventArgs e)
{
// If all conditions have been met, clear the ErrorProvider of errors.
errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
// Confirm that the e-mail address string is not empty.
if(emailAddress.Length == 0)
{
errorMessage = "e-mail address is required.";
return false;
}

// Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
if(emailAddress.IndexOf("@") > -1)
{
if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
{
errorMessage = "";
return true;
}
}

errorMessage = "e-mail address must be valid e-mail address format.\n" +
"For example 'someone@example.com' ";
return false;
}



++++++++++++++++++++++++++++++++++++++++++++++++++


textbox validation accepts number only
++++++++++++++++++++++++++++++++++++++++



private void textBox3_Validating(object sender, CancelEventArgs e)
{
try
{
int numberEntered = int.Parse(textBox3.Text);
if (numberEntered < 1 || numberEntered > 10)
{
e.Cancel = true;
MessageBox.Show("You have to enter a number between 1 and 10");
}
}
catch (FormatException)
{
e.Cancel = true;
MessageBox.Show("You need to enter an integer");
}
}



++++++++++++++++++++++++++++++++++++++++++++++++++++++++


only numbers and a dot
+++++++++++++++++++++++++++++++++++


private void textBox2_TextChanged(object sender, EventArgs e)
{

if (!(string.IsNullOrEmpty(textBox2.Text)))
{

char[] chars = textBox2.Text.ToCharArray();
for (int i = 0; i < textBox2.Text.Length; i++)
{
int code;
code = Convert.ToInt16(chars [i] );
if (!((!(code > 58 || code < 48)) || (code == 68) || (code == 78) || (code == 35) || (code == 46)))
{
textBox2.Text = textBox2.Text.Remove(i, 1);
textBox2.SelectionStart = textBox2.Text.Length;
}
}
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

e-mail validation

++++++++++++++++++++++++++++


private void textBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (textBox1.Text.Length > 0)
{
if (!rEMail.IsMatch(textBox1.Text))
{
MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.SelectAll();
e.Cancel = true;
}
}
}

+++++++++++++++++++++++++++++++++++++++


TEXTBOX ONLY ACCEPTS CHARACTERS
++++++++++++++++++++++++++++++++++

private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || e.KeyChar == 32 || e.KeyChar == 8)
{
e.Handled = false;
}
else
{
MessageBox.Show("Invalid Input");
e.Handled = true;
}
}
+++++++++++++++++++++++++++++++++++++++


Found interesting? Add this to:



Comments or Responses


Login to post response