Help on Regular Expression

J

John

I am new in Regular Expression. Could someone please help me in following
expression?
1. the string cannot be empty
2. the string can only contains AlphaNumeric characters. No space or any
special characters are allowed
3. space characters at the end of string is ok
4. the string cannot contains only numeric characters, in other word, the
string must contains a least one alpha character

Thanks for the help
 
G

Guest

John:

The following code will check for a string that is 1-20 characters long. It
does not specifically test Condition 3 but you could trim the string and then
check it.

Private Function CheckStringRules(ByVal psString) As _Outcome

Dim lsPattern As String
Dim mc As MatchCollection
Dim m As Match

'Check for allowed character set; the length has to be 1-20
lsPattern = "^([a-zA-Z0-9|]{1,20})$"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of number
lsPattern = "[0-9]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of upper-alpha character
lsPattern = "[A-Z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of lower-alpha character
lsPattern = "[a-z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

Return _Outcome.Successful

End Function
 
J

John

Thanks vvenk for your reply.
What I want to do is to add a RegularExpressionValidator control in the form
and specify a RegularExpression for the Validator control and validate
against a textbox in client side.
Is that possible to combine all the requirement into one Regular Expression,
so that I don't need to go to code-behind to do the validation?
Thanks again.


vvenk said:
John:

The following code will check for a string that is 1-20 characters long. It
does not specifically test Condition 3 but you could trim the string and then
check it.

Private Function CheckStringRules(ByVal psString) As _Outcome

Dim lsPattern As String
Dim mc As MatchCollection
Dim m As Match

'Check for allowed character set; the length has to be 1-20
lsPattern = "^([a-zA-Z0-9|]{1,20})$"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of number
lsPattern = "[0-9]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of upper-alpha character
lsPattern = "[A-Z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of lower-alpha character
lsPattern = "[a-z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

Return _Outcome.Successful

End Function

John said:
I am new in Regular Expression. Could someone please help me in following
expression?
1. the string cannot be empty
2. the string can only contains AlphaNumeric characters. No space or any
special characters are allowed
3. space characters at the end of string is ok
4. the string cannot contains only numeric characters, in other word, the
string must contains a least one alpha character

Thanks for the help
 
R

Robby

Aren't Regular Expressions powerful ...

(?i)^([a-z]|\d)+((?<=\d)[a-z]|(?<=[a-z]))[a-z0-9]*

case insensitive
start of string
match one or more letters OR one or more digits
continue matching if previous is
one digit followed by one letter
OR
one letter
match zero or more letters and digits

--Robby


John said:
Thanks vvenk for your reply.
What I want to do is to add a RegularExpressionValidator control in the
form
and specify a RegularExpression for the Validator control and validate
against a textbox in client side.
Is that possible to combine all the requirement into one Regular
Expression,
so that I don't need to go to code-behind to do the validation?
Thanks again.


vvenk said:
John:

The following code will check for a string that is 1-20 characters long. It
does not specifically test Condition 3 but you could trim the string and then
check it.

Private Function CheckStringRules(ByVal psString) As _Outcome

Dim lsPattern As String
Dim mc As MatchCollection
Dim m As Match

'Check for allowed character set; the length has to be 1-20
lsPattern = "^([a-zA-Z0-9|]{1,20})$"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of number
lsPattern = "[0-9]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of upper-alpha character
lsPattern = "[A-Z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of lower-alpha character
lsPattern = "[a-z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

Return _Outcome.Successful

End Function

John said:
I am new in Regular Expression. Could someone please help me in following
expression?
1. the string cannot be empty
2. the string can only contains AlphaNumeric characters. No space or
any
special characters are allowed
3. space characters at the end of string is ok
4. the string cannot contains only numeric characters, in other word, the
string must contains a least one alpha character

Thanks for the help
 
R

Robby

opps ... Forgot to check remaining characters are spaces.

(?i)^([a-z]|\d)+((?<=\d)[a-z]|(?<=[a-z]))[a-z0-9]* *$

That is a space between the two *
" *" matches zero or more spaces
$ matches end of string

--Robby

Robby said:
Aren't Regular Expressions powerful ...

(?i)^([a-z]|\d)+((?<=\d)[a-z]|(?<=[a-z]))[a-z0-9]*

case insensitive
start of string
match one or more letters OR one or more digits
continue matching if previous is
one digit followed by one letter
OR
one letter
match zero or more letters and digits

--Robby


John said:
Thanks vvenk for your reply.
What I want to do is to add a RegularExpressionValidator control in the
form
and specify a RegularExpression for the Validator control and validate
against a textbox in client side.
Is that possible to combine all the requirement into one Regular
Expression,
so that I don't need to go to code-behind to do the validation?
Thanks again.


vvenk said:
John:

The following code will check for a string that is 1-20 characters long. It
does not specifically test Condition 3 but you could trim the string and then
check it.

Private Function CheckStringRules(ByVal psString) As _Outcome

Dim lsPattern As String
Dim mc As MatchCollection
Dim m As Match

'Check for allowed character set; the length has to be 1-20
lsPattern = "^([a-zA-Z0-9|]{1,20})$"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of number
lsPattern = "[0-9]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of upper-alpha character
lsPattern = "[A-Z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of lower-alpha character
lsPattern = "[a-z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

Return _Outcome.Successful

End Function

:

I am new in Regular Expression. Could someone please help me in following
expression?
1. the string cannot be empty
2. the string can only contains AlphaNumeric characters. No space or
any
special characters are allowed
3. space characters at the end of string is ok
4. the string cannot contains only numeric characters, in other word, the
string must contains a least one alpha character

Thanks for the help
 
J

John

Robby,
I am really appreciated for your help!

Robby said:
opps ... Forgot to check remaining characters are spaces.

(?i)^([a-z]|\d)+((?<=\d)[a-z]|(?<=[a-z]))[a-z0-9]* *$

That is a space between the two *
" *" matches zero or more spaces
$ matches end of string

--Robby

Robby said:
Aren't Regular Expressions powerful ...

(?i)^([a-z]|\d)+((?<=\d)[a-z]|(?<=[a-z]))[a-z0-9]*

case insensitive
start of string
match one or more letters OR one or more digits
continue matching if previous is
one digit followed by one letter
OR
one letter
match zero or more letters and digits

--Robby


John said:
Thanks vvenk for your reply.
What I want to do is to add a RegularExpressionValidator control in the
form
and specify a RegularExpression for the Validator control and validate
against a textbox in client side.
Is that possible to combine all the requirement into one Regular
Expression,
so that I don't need to go to code-behind to do the validation?
Thanks again.


John:

The following code will check for a string that is 1-20 characters
long.
It
does not specifically test Condition 3 but you could trim the string
and
then
check it.

Private Function CheckStringRules(ByVal psString) As _Outcome

Dim lsPattern As String
Dim mc As MatchCollection
Dim m As Match

'Check for allowed character set; the length has to be 1-20
lsPattern = "^([a-zA-Z0-9|]{1,20})$"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of number
lsPattern = "[0-9]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of upper-alpha character
lsPattern = "[A-Z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

'Check for atleast one occurence of lower-alpha character
lsPattern = "[a-z]"
mc = Regex.Matches(psString, lsPattern)
If mc.Count = 0 Then
Return _Outcome.Failure
End If

Return _Outcome.Successful

End Function

:

I am new in Regular Expression. Could someone please help me in
following
expression?
1. the string cannot be empty
2. the string can only contains AlphaNumeric characters. No space or
any
special characters are allowed
3. space characters at the end of string is ok
4. the string cannot contains only numeric characters, in other word,
the
string must contains a least one alpha character

Thanks for the help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top