Regular expression for password validation in .net

Following example show that how we can validate password with the help of Regular Expression that have following validation in and c# languages :

-Must be at least 10 characters
-Must contain at least one one lower case letter,
-One upper case letter,
-One digit and one special character
-Valid special characters are – @#$%^&+=

(Passwords like ‘asWE$21@ut’, WER@#%345ertO’ etc are the valid password entries according to following code.)


  1. Private Sub Button1_Click(ByVal Sender As Object, _
  2.                               ByVal e As System.EventArgs) Handles Button1.Click
  3.         Dim MatchNumberPattern As String = "^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"
  4.         If TextBox2.Text.Trim <> "" Then
  5.             If Not Regex.IsMatch(TextBox2.Text, MatchNumberPattern) Then
  6.                 MessageBox.Show("Password is not valid")
  7.             End If
  8.         End If
  9. End Sub


  1. private void Button1_Click(object Sender, System.EventArgs e)
  2. {
  3.  string MatchNumberPattern = "^.*(?=.{10,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$";
  4.  if (!string.IsNullOrEmpty(TextBox2.Text.Trim)) {
  5.   if (!Regex.IsMatch(TextBox2.Text, MatchNumberPattern)) {
  6.    MessageBox.Show("Password is not valid");
  7.   }
  8.  }
  9. }


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=""> <strike> <strong>