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 vb.net 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.)

Code:
[vb.net]

  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

[c#]

  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. }

facebooktwittergoogle_plusredditpinterestlinkedinmail