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]
Private Sub Button1_Click(ByVal Sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim MatchNumberPattern As String = "^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$" If TextBox2.Text.Trim <> "" Then If Not Regex.IsMatch(TextBox2.Text, MatchNumberPattern) Then MessageBox.Show("Password is not valid") End If End If End Sub
[c#]
private void Button1_Click(object Sender, System.EventArgs e) { string MatchNumberPattern = "^.*(?=.{10,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"; if (!string.IsNullOrEmpty(TextBox2.Text.Trim)) { if (!Regex.IsMatch(TextBox2.Text, MatchNumberPattern)) { MessageBox.Show("Password is not valid"); } } }