By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Help on Regular Expression

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" wrote:
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

Nov 21 '05 #2

P: n/a
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" <vv***@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
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" wrote:
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

Nov 21 '05 #3

P: n/a
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" <Jo**@hotmail.com> wrote in message
news:um*************@TK2MSFTNGP11.phx.gbl...
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" <vv***@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
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" wrote:
> 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
>
>
>


Nov 21 '05 #4

P: n/a

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" <ed****@not.my.email.com> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
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" <Jo**@hotmail.com> wrote in message
news:um*************@TK2MSFTNGP11.phx.gbl...
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" <vv***@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
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" wrote:

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



Nov 21 '05 #5

P: n/a
Robby,
I am really appreciated for your help!

"Robby" <ed****@not.my.email.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...

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" <ed****@not.my.email.com> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
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" <Jo**@hotmail.com> wrote in message
news:um*************@TK2MSFTNGP11.phx.gbl...
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" <vv***@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
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" wrote:

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



Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.