Hello Friends...

I need help on regular expressions....

I tried the following way but it did not work for me....please help me to create a regular expression...

^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(^[a-zA-Z0-9@\$=!:.#%]+$)

My requirement is
-> Min 8 to 15 characters - this I will do with the length validation or by including in the regex
->
atleast one uppercase character ,
atleast one number ,
atleast one lower case character
and only one special case character

other requirement is
-> only lower case characters (min 8 - max 15) Regular Expression - [a-z]{8,15}

Please let me know if I am correct else please correct me....

Edited 4 Years Ago by poojavb

What you are describing is more of an irregular expression. Your character classes need not be in any particular order. I believe you are far better off handling the validation in code. This will be clearer and more maintainable. Also, your requirements appear to be in conflict

atleast one uppercase character

and

only lower case characters

You can't have both. Also, a minimum must be a specific number. It cannot be a range. You can have a minimium of 8 but not of 8-15. I presume you mean the length must be between 8 and 15 characters.

U r getting it wrong....
there are two places where I need the regular expressions.....

for password I want a strong password with following criteria

range between 8 to 15
atleast one uppercase character ,
atleast one number ,
atleast one lower case character
and only one special case character

for username I want only alphabets....

only lower case characters (min 8 - max 15)

I hope I am clear now.....please help me to create this regular expression.....

I assume you are trying to create a code for checking the password strenght. Here is some of my code I used to have for it:

Private Enum PasswordScore
    Blank = 0
    VeryWeak = 1
    Weak = 2
    Medium = 3
    Strong = 4
    VeryStrong = 5
End Enum

Private Shared Function CheckingPasswordStrength(password As String) As PasswordScore
    Dim score As Integer = 1
    If password.Length < 1 Then
        Return PasswordScore.Blank
    End If
    If password.Length < 4 Then
        Return PasswordScore.VeryWeak
    End If

    If password.Length >= 8 Then
        score += 1
    End If
    If password.Length >= 12 Then
        score += 1
    End If
    If Regex.IsMatch(password, "[0-9]+(\.[0-9][0-9]?)?", RegexOptions.ECMAScript) Then
        'number only //"^\d+$" if you need to match more than one digit.
        score += 1
    End If
    If Regex.IsMatch(password, "^(?=.*[a-z])(?=.*[A-Z]).+$", RegexOptions.ECMAScript) Then
        'both, lower and upper case
        score += 1
    End If
    If Regex.IsMatch(password, "[!,@,#,$,%,^,&,*,?,_,~,-,L,(,)]", RegexOptions.ECMAScript) Then
        '^[A-Z]+$
        score += 1
    End If
    Return CType(score, PasswordScore)
End Function

I dont want to check if its strong or no but when the user will enter the details in the textbox I want to make sure that he/she wont enter wrong data....

eg my criteria for password is as follows

range between 8 to 15
atleast one uppercase character ,
atleast one number ,
atleast one lower case character
and only one special case character

consider for special case....
only one special case is allowed....so if the validating event finds two or more it should prompt that the entry is wrong,,,,

How about this then

Imports System.Text.RegularExpressions

Public Class Form1
.
.
.
    Private Function ValidPassword(password As String) As Boolean

        'checks for
        '
        '  invalid length
        '  no lower case letters
        '  no upper case letters
        '  no digits
        '  more than one special character

        If Len(password) < 8 Or Len(password) > 15 Then Return False 'invalid length
        If Not Regex.IsMatch(password, "[a-z]") Then Return False 'no lower case letter
        If Not Regex.IsMatch(password, "[A-Z]") Then Return False 'no upper case letter
        If Not Regex.IsMatch(password, "[0-9]") Then Return False 'no digit
        If Regex.Matches(password, "[!,@,#,$,%,^,&,*,?,_,~,-,L,(,)]").Count > 1 Then Return False 'too many special case

        Return True

    End Function
.
.
.
End Class

Thank u all for ur help....

Reverend Jim - Can the expression which u have given be converted to one and then be applied on textbox validating event that will prompt the error....

see my example that I have done for email....can the same be applied to password....

     Private Sub txtEmailID_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtEmailID.Validating
        If txtEmailID.Text <> "" Then
            Dim rex As Match = Regex.Match(Trim(txtEmailID.Text), "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$", RegexOptions.IgnoreCase)
            If rex.Success = False Then
                ErrorProvider1.SetError(txtEmailID, "Please Enter a valid Email-Address")
                txtEmailID.Focus()
                Exit Sub
            Else
                ErrorProvider1.Clear()
            End If
        End If
    End Sub

The following code might do it

    Private Function CheckPassword(password As String) As String

        If Len(password) < 8 Or Len(password) > 15 Then Return "not 8-15 chars"
        If Not Regex.IsMatch(password, "[a-z]") Then Return "no lower case letters"
        If Not Regex.IsMatch(password, "[A-Z]") Then Return "no upper case letters"
        If Not Regex.IsMatch(password, "[0-9]") Then Return "no digits"
        If Regex.Matches(password, "[!,@,#,$,%,^,&,*,?,_,~,-,L,(,)]").Count > 1 Then Return "more than one special char"

        Return "OK"

    End Function

    Private Sub txtTestStr_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles txtTestStr.Validating

        Dim result = CheckPassword(txtTestStr.Text)

        If result = "OK" Then
            e.Cancel = False
        Else
            MsgBox(result)
            e.Cancel = True
        End If
    End Sub

I don't generally like doing things this way because it locks the user into a field until the criteria are satisfied. An alternative would be to add a label next to the password entry field and modify the text in the label to reflect the current password status (could be updated on each keystroke).

This question has already been answered. Start a new discussion instead.