You are on page 1of 6

AIM: - Develop Windows Application in C# to perform all the validations without using any validator.

FORM 1 using using using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Text.RegularExpressions; System.Windows.Forms.ComponentModel; System.Globalization;

namespace WindowsFormsApplication2 { public partial class Form1 : Form { public string str1, str2, str3; public int age, dob, dob1, dob2; public Form1() { InitializeComponent(); } private void btnvalidate_Click(object sender, EventArgs e) { try { str1 = requiredfieldvalidator.Text; str2 = comparevalidator.Text; str3 = regularexpvalidator.Text; age = Convert.ToInt32(rangevalidator.Text); dob = Convert.ToInt32(customvalidator.Text); dob1 = Convert.ToInt32(textBox2.Text); dob2 = Convert.ToInt32(textBox1.Text); if (requiredfieldvalidator.Text != comparevalidator.Text) { label8.Show(); label8.Text = "Name and User Name Must Be Same!!!!"; return; } int n = int.Parse(rangevalidator.Text.Trim()); if (n < 1 || n > 100) { MessageBox.Show("Age must be in the range of 1 to 100"); label9.Show();

label9.Text = "Input Age must be in the Range of 1 to 100!!!"; } else { label9.Hide(); } MessageBox.Show("Registration Completed!!!!!"); this.Hide(); Form2 obj = new Form2(str1, str2, age, str3, dob, dob1, dob2); obj.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

private void requiredfieldvalidator_KeyPress(object sender, KeyPressEventArgs e) { // 97 to 122 is for a to z ///32 for blank space /// 48 to 57 is for numeric values /// 8 is for back space if (((int)e.KeyChar >= 65 && (int)e.KeyChar <= 90) || ((int)e.KeyChar >= 97 && (int)e.KeyChar <= 122) || ((int)e.KeyChar) == 8 || ((int)e.KeyChar) == 32) { e.Handled = false; } else { MessageBox.Show("Please Enter Alphabets Only!!"); e.Handled = true; } } private void comparevalidator_KeyPress(object sender, KeyPressEventArgs e) { if (((int)e.KeyChar >= 65 && (int)e.KeyChar <= 90) || ((int)e.KeyChar >= 97 && (int)e.KeyChar <= 122) || ((int)e.KeyChar) == 8 || ((int)e.KeyChar) == 32) { e.Handled = false; } else { MessageBox.Show("Please Enter Alphabets Only!!"); e.Handled = true; } } private void requiredfieldvalidator_Validating(object sender, CancelEventArgs e)

{ if (requiredfieldvalidator.Text == "") { TextBox textbox = sender as TextBox; e.Cancel = string.IsNullOrWhiteSpace(textbox.Text); errorProvider1.SetError(textbox, "Textbox cannot be empty"); label7.Show(); label7.Text = "Name Cannot Be Empty!!!"; } else { label7.Hide(); errorProvider1.Clear(); } } private void Form1_Load(object sender, EventArgs e) { label7.Hide(); label8.Hide(); label9.Hide(); label10.Hide(); label11.Hide(); } private void comparevalidator_Validating(object sender, CancelEventArgs e) { if (comparevalidator.Text == "") { TextBox textbox = sender as TextBox; e.Cancel = string.IsNullOrWhiteSpace(textbox.Text); errorProvider1.SetError(textbox, "Textbox cannot be empty"); label8.Show(); label8.Text = " User Name Cannot Be Empty!!!"; } else { label8.Hide(); errorProvider1.Clear(); } } private void rangevalidator_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; MessageBox.Show("Please Enter Only Numbers!!!!"); } } private void regularexpvalidator_Validating(object sender, CancelEventArgs e)

{ if (regularexpvalidator.Text == "") { label10.Show(); label10.Text = "E-mail ID can't be blank!!!!"; errorProvider1.SetError(regularexpvalidator,"Can't be blank"); } else { label10.Hide(); errorProvider1.Clear(); 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 (regularexpvalidator.Text.Length > 0) { if (!rEMail.IsMatch(regularexpvalidator.Text)) { MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); regularexpvalidator.SelectAll(); label10.Show(); label10.Text = "Please Enter Valid E-mail ID"; e.Cancel = true; } } } } private void customvalidator_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ) { e.Handled = true; MessageBox.Show("Please Enter Only Numbers!!!!"); } } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ) { e.Handled = true; MessageBox.Show("Please Enter Only Numbers!!!!"); } } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) ) { e.Handled = true;

MessageBox.Show("Please Enter Only Numbers!!!!"); } } private void rangevalidator_Validating(object sender, CancelEventArgs e) { if (rangevalidator.Text == "") { label9.Show(); label9.Text = "Please Enter Age"; errorProvider1.SetError(rangevalidator, "Please Enter Age"); } else { label9.Hide(); errorProvider1.Clear(); } } } } FORM 2 using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace WindowsFormsApplication2 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private string name1, dname1,email1; int age1, dobb, dobb1, dobb2; public Form2(string name,string dname,int age,string email,int dob,int dob1,int dob2) { InitializeComponent(); this.name1 = name; this.dname1= dname; this.age1 = age; this.email1= email; this.dobb = dob; this.dobb1 = dob1;

this.dobb2=dob2; } private void Form2_Load(object sender, EventArgs e) { label1.Text = this.name1; label7.Text = this.dname1; label8.Text = this.age1.ToString(); label9.Text = this.email1; label10.Text = this.dobb.ToString() + "/"+ this.dobb1.ToString() +"/"+ this.dobb2.ToString(); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Application.Exit(); } } }

You might also like