How to Validate a Registration Form Using Regular Expression in C#

In this tutorial, I will teach you how to validate a Registration Form using Regular Expression in C#. With this method, you can restrict all the textboxes in a form.It is also a guide for all users when they're going to fill up a registration form.

Let’s get started:

Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. Then, do the following Form as follows. fig 1 Go to the Solution Explorer, click the “View Code” to make the code editor appear. fig 2 In the code editor, put the "using System.Text.RegularExpressions;" above the namespace to access regular expression library.
  1. //import the regular expression library
  2. using System.Text.RegularExpressions;
After that, do the following codes for formating the specific fields using the correct pattern.
  1. //initialize the validating method
  2. static Regex Valid_Name = StringOnly();
  3. static Regex Valid_Contact = NumbersOnly();
  4. static Regex Valid_Password = ValidPassword();
  5. static Regex Valid_Email = Email_Address();
  6.  
  7. //Method for validating email address
  8. private static Regex Email_Address()
  9. {
  10. string Email_Pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
  11. + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
  12. + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
  13.  
  14. return new Regex(Email_Pattern, RegexOptions.IgnoreCase);
  15. }
  16. //Method for string validation only
  17. private static Regex StringOnly()
  18. {
  19. string StringAndNumber_Pattern = "^[a-zA-Z]";
  20.  
  21. return new Regex(StringAndNumber_Pattern, RegexOptions.IgnoreCase);
  22. }
  23. //Method for numbers validation only
  24. private static Regex NumbersOnly()
  25. {
  26. string StringAndNumber_Pattern = "^[0-9]*$";
  27.  
  28. return new Regex(StringAndNumber_Pattern, RegexOptions.IgnoreCase);
  29. }
  30. //Method for password validation only
  31. private static Regex ValidPassword()
  32. {
  33. string Password_Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,15})$";
  34.  
  35. return new Regex(Password_Pattern, RegexOptions.IgnoreCase);
  36. }
Go back to the design view, double-click the button and do the following codes to validate the registration form.
  1. //Validate all textboxes when the button is clicked
  2.  
  3. private void btnSave_Click_1(object sender, EventArgs e)
  4. {
  5. //for Name
  6. if (Valid_Name.IsMatch(txtName.Text) != true)
  7. {
  8. MessageBox.Show("Namme accepts only alphabetical characters", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  9. txtName.Focus();
  10. return;
  11. }
  12.  
  13. //for Address
  14. if (txtAddress.Text == "")
  15. {
  16. MessageBox.Show("Address cannot be empty!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  17. txtAddress.Focus();
  18. return;
  19. }
  20. //for Contacts
  21. if (Valid_Contact.IsMatch(txtContactNo.Text) != true)
  22. {
  23. MessageBox.Show("Contact accept numbers only.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  24. txtContactNo.Focus();
  25. return;
  26. }
  27. //for username
  28. if (txtUsername.Text == "")
  29. {
  30. MessageBox.Show("Username cannot be empty!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  31. txtUsername.Focus();
  32. return;
  33. }
  34. //for password
  35. if (Valid_Password.IsMatch(txtPassword.Text) != true)
  36. {
  37. MessageBox.Show("Password must be atleast 8 to 15 characters. It contains atleast one Upper case and numbers.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  38. txtPassword.Focus();
  39. return;
  40. }
  41. //for Email Address
  42. if (Valid_Email.IsMatch(txtEmailAddress.Text) != true)
  43. {
  44. MessageBox.Show("Invalid Email Address!","Invalid",MessageBoxButtons.OK ,MessageBoxIcon.Exclamation );
  45. txtEmailAddress.Focus();
  46. return;
  47. }
  48.  
  49. //success message
  50. MessageBox.Show("You are now successfully registered.");
  51.  
  52. //hidding all object in the form
  53. foreach(Control txt in this.Controls)
  54. {
  55. if (txt is Control)
  56. {
  57. txt.Visible = false;
  58.  
  59. }
  60. }
  61.  
  62. }
For all students who need a programmer for your thesis system or anyone who needs a source code in any programming languages. You can contact me @ : Email – [email protected] Mobile No. – 09305235027 – tnt

Add new comment