Thursday, 3 October 2013

How to validated windows Application Form with user input and Regular Expressions

Validating user input with Regular Expressions and Linq in Windows Application....................





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.Text.RegularExpressions;

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

private void btnOk_Click(object sender, EventArgs e)
{
// find blank TextBoxs and orderby TabIndex with Linq Concept.....................

var emptytextbox = from Control CurrentControl in Controls
where CurrentControl is TextBox
let box = CurrentControl as TextBox
where String.IsNullOrEmpty(box.Text)
orderby box.TabIndex
select box;

// if there is empty textbox........
if (emptytextbox.Count() > 0)
{
// display message box indicating message information.........................
MessageBox.Show("Please fill the all TextBox Field", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
//select first empty TextBox...............
emptytextbox.First().Select();
}
else
{
if (!ValidateInput(txtfname.Text, "^[A-Z][a-zA-Z]*$", "Your First Name is not valid format"))
{
txtfname.Select();
}
else if (!ValidateInput(txtlname.Text, "^[A-Z][a-zA-Z]*$", "Your Last Name is not valid format"))
{
txtlname.Select();
}
else if (!ValidateInput(txtemailid.Text, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", "Your emailid is not valid format"))

{
txtemailid.Select();
}
else if (!ValidateInput(txtaddress.Text, @"^[0-9]+\s+([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$", "Your Address is not valid format"))
{
txtaddress.Select();
}
else if (!ValidateInput(txtcityname.Text, @"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$", "Your City Name is not valid format"))
{
txtcityname.Select();
}
else if (!ValidateInput(txtstatename.Text, @"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$", "Your State Name is not valid format"))
{
txtstatename.Select();
}
// Phone numbers like 0548-2221005
else if (!ValidateInput(txtphoneno.Text, @"^[0-9]\d{2,4}-\d{6,8}$", "Your Phone No is not valid format"))
{
txtphoneno.Select();
}
else if (!ValidateInput(txtzipcode.Text, @"^\d{6}$", "Please enter valid Zip-Code like-233001"))
{
txtzipcode.Select();
}
// Mobile numbers starting from 7,8 and 9 and not from 0,6...
else if (!ValidateInput(txtmobileno.Text, @"^[789]\d{9}$", "Please enter valid Mobile No like- 9451119029"))
{
txtmobileno.Select();
}
else
{
this.Hide();
MessageBox.Show("Thank You", "Correct Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();

}
}
}

//user regular expressions to validate user input.....................

private bool ValidateInput(string input,string expression,string message)
{
//store whether the input is valid
bool valid = Regex.Match(input, expression).Success;
//if the input doesn't match the regular expression......
if (!valid)
{
//input was invalid......
MessageBox.Show(message, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return valid;
}
}
}




2 comments: