«

»

Dec 24

validations in windows application in c#.net

[code language=”csharp”]

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Data.SqlClient;

using System.Text.RegularExpressions;

namespace FecilitiesMgt

{

public partial class AddNewUser : Form

{

public AddNewUser()

{

InitializeComponent();

}

SqlConnection con = new SqlConnection("uid=sa;password=123;database=FacilityMgt");

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

{

Form f = new AdminMenu();

f.Show();

this.Hide();

}

private void btnsubmit_Click(object sender, EventArgs e)

{

if (txtname.Text == "" || txtprsno.Text == "" || txtrank.Text == "" || txtcont.Text == "" || txtmail.Text == "" || txtuser.Text == "" || txtpwd.Text == "")

{

MessageBox.Show(this, "Enter all fields", "Facility", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

string s = "insert into adduser values(‘" + txtname.Text + "’,’" + txtprsno.Text + "’,’" + txtrank.Text + "’,’" + txtcont.Text + "’,’" + txtmail.Text + "’,’" + txtuser.Text + "’,’" + txtpwd.Text + "’)";

SqlCommand cmd = new SqlCommand(s, con);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show("User added");

txtname.Text = txtprsno.Text = txtrank.Text = txtcont.Text = txtmail.Text = txtuser.Text = txtpwd.Text = "";

}

}

private void txtcont_KeyPress(object sender, KeyPressEventArgs e)

{

if (char.IsDigit(e.KeyChar) == false)

{

MessageBox.Show("Enter digit Only");

e.Handled = true;

}

}

private void txtname_Leave(object sender, EventArgs e)

{

if (txtname.Text == "")

{

MessageBox.Show("Enter name");

txtname.Focus();

}

}

private void txtprsno_KeyPress(object sender, KeyPressEventArgs e)

{

if (char.IsDigit(e.KeyChar) == false)

{

MessageBox.Show("Enter digit Only");

e.Handled = true;

}

}

private void txtuser_Leave(object sender, EventArgs e)

{

if (txtuser.Text.Length < 6)

{

MessageBox.Show(this, "username should not be less than 6 characters", "username", MessageBoxButtons.OK, MessageBoxIcon.Warning);

txtuser.Focus();

}

}

private void txtpwd_Leave(object sender, EventArgs e)

{

if (txtpwd.Text.Length < 6)

{

MessageBox.Show(this, "password: min 6 characters ", "password", MessageBoxButtons.OK, MessageBoxIcon.Warning);

txtpwd.Focus();

}

}

private void txtcont_Leave(object sender, EventArgs e)

{

if (txtcont.Text == "")

{

MessageBox.Show("Enter Mobile number");

txtcont.Focus();

}

}

//private void AddNewUser_Load(object sender, EventArgs e)

//{

//}

private void txtname_TextChanged(object sender, EventArgs e)

{

}

private void txtprsno_Leave(object sender, EventArgs e)

{

if (txtprsno.Text == "")

{

MessageBox.Show("Enter personal no");

txtprsno.Focus();

}

}

private void txtrank_Leave(object sender, EventArgs e)

{

if (txtrank.Text == "")

{

MessageBox.Show("Enter Rank");

txtrank.Focus();

}

}

private void txtmail_Leave(object sender, EventArgs e)

{

//if (txtmail.Text == "")

//{

//    MessageBox.Show("enter Email-ID");

//    txtmail.Focus();

//}

if (!Regex.Match(txtmail.Text, @"w+([-+.’]w+)*@w+([-.]w+)*.w+([-.]w+)*").Success)

{

MessageBox.Show(txtmail.Text+ " is Invalid Email");

txtmail.Focus();

}

}

private void txtcont_Validating(object sender, CancelEventArgs e)

{

if (txtcont.Text.Length != 10)

{

MessageBox.Show(this, "contact number must 10 numbers", "Contact", MessageBoxButtons.OK, MessageBoxIcon.Warning);

txtcont.Focus();

}

}

private void lblname_Click(object sender, EventArgs e)

{

}

private void txtname_KeyPress(object sender, KeyPressEventArgs e)

{

if (char.IsLetter(e.KeyChar) == false)

{

MessageBox.Show("Enter text only");

txtname.Focus();

}

}}

[/code]

8 comments

Skip to comment form

  1. Dersana

    Really good

    1. Anonymous

      Thanq Dersana.

  2. Naseer

    Thanq Dersana.

  3. Anonymous

    Thank you . 🙂

  4. Munna yadav

    your code of validating of email is not working properly,,,, while i am putting valid email formet, then also it shows the message………

    private void txtEmailid_Leave(object sender, EventArgs e)
    {
    if (!Regex.Match(txtEmailid.Text, @”w+([-+.’]w+)*@w+([-.]w+)*.w+([-.]w+)*”).Success)
    {

    MessageBox.Show(txtEmailid.Text + ” is Invalid Email Id”);

    txtEmailid.Focus();
    return;

    }
    }

  5. Pratibha

    really nice n very much benificial for beginers….liked it

  6. Anonymous

    really good thanks a lot

  7. Anonymous

    good

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>