C# .NET - how can i validate textbox control in window application

Asked By james vasanth on 07-May-09 07:59 AM
help me to  validate the numbers, letters and email id's

System.Text.RegularExpressions.Regex.IsMatch(stringvalue , @"[0-9]"))

Web Star replied to james vasanth on 07-May-09 08:06 AM
end of post

javascript validation

Rao B replied to james vasanth on 07-May-09 08:08 AM

script language = "Javascript">
/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
     alert("Invalid E-mail ID")
     return false
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     alert("Invalid E-mail ID")
     return false
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      alert("Invalid E-mail ID")
      return false
  }

   if (str.indexOf(at,(lat+1))!=-1){
      alert("Invalid E-mail ID")
      return false
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      alert("Invalid E-mail ID")
      return false
   }

   if (str.indexOf(dot,(lat+2))==-1){
      alert("Invalid E-mail ID")
      return false
   }
  
   if (str.indexOf(" ")!=-1){
      alert("Invalid E-mail ID")
      return false
   }

    return true     
 }

function ValidateForm(){
 var emailID=document.frmSample.txtEmail
 
 if ((emailID.value==null)||(emailID.value=="")){
  alert("Please Enter your Email ID")
  emailID.focus()
  return false
 }
 if (echeck(emailID.value)==false){
  emailID.value=""
  emailID.focus()
  return false
 }
 return true
 }
</script>


check this link

http://www.smartwebby.com/DHTML/phone_no_validation.asp


validate email in window application as follows

Web Star replied to james vasanth on 07-May-09 08:09 AM
 

using System.Text.RegularExpressions;



string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +

@"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" +

@"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";

System.Text.RegularExpressions.Match match =

Regex.Match(email, pattern, RegexOptions.IgnoreCase);

if (!match.Success)

{

// error in address

}

http://social.microsoft.com/Forums/en-US/netfxnetcom/thread/c3c4130b-d2a1-4ad1-a24c-dcf3cab3070b

Santhosh N replied to james vasanth on 07-May-09 08:10 AM

 Numbers:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{

if (!Char.IsNumber(e.KeyChar))
{
MessageBox.Show("PLease Enter Numeric Value");
e.Handled = true ;
}
}

Email:

To validate a email id in textbox you can place this code in Validating event of textbox or in Leave event of textbox and make use of  the regex classes which are located in the namespace System.Text.RegularExpressions


 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 (txtmail.Text.Length > 0){


                if (!rEMail.IsMatch(txtmail.Text))
                {
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtmail.SelectAll();
                    e.Cancel = true;
                }



here are the validation methods
H K replied to james vasanth on 07-May-09 09:48 AM
using system.Text;
// validation for letters
RegularExpressions.Regex RegExp = new RegularExpressions.Regex(@"^[a-zA-Z]$");

            if (txtInput.Text.Length > 0){


                if (!RegExp .IsMatch(txtInput.Text))
                {
                    MessageBox.Show("Enter valid data");
                    txtInput.focus();
                 }

}

Validation for number
RegularExpressions.Regex RegExp = new RegularExpressions.Regex(@"^[0-9]$");

Validation for email:
RegularExpressions.Regex RegExp = new RegularExpressions.Regex(@"
^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]\                               .)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")

Solution >> how can i validate textbox control in window application
DC RC replied to james vasanth on 07-May-09 01:30 PM
Try this link,

http://www.java2s.com/Code/VB/GUI/TextBoxvalidationvalidateinValidatingEvent.htm
http://www.codeproject.com/KB/edit/textboxregex.aspx
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/93b8bf6c-ba84-4084-95aa-3f03c210a70c

Thank You