Board index » Web Programming » Usename regex

Usename regex

Web Programming393
Hi All,



I would like to know how I can limit users to only registering usernames

which have alphanumberic characters and one underscore.

From what I understand is I can use a regex to do this. But I have no

idea how to do it. I have only used a customfieldvalidor before. And the

reason I cant do it now is because of the design of the page itself.



So basically I need to know how, upon a button click, I can compare the

text entered in a textbox, to a regex, if its invalid throw a error, if

its valid proceed.



Thanks.


-
 

Re:Usename regex

Hello Mick,



Quote
Hi All,



I would like to know how I can limit users to only registering

usernames

which have alphanumberic characters and one underscore.

From what I understand is I can use a regex to do this. But I have no

idea how to do it. I have only used a customfieldvalidor before. And

the

reason I cant do it now is because of the design of the page itself.



So basically I need to know how, upon a button click, I can compare

the text entered in a textbox, to a regex, if its invalid throw a

error, if its valid proceed.



Thanks.





You can indeed use Regex for this. It's actually a simple expression:



because a regex describes the input from left to right you need to account

for all possible locations of the single '_' you're allowing.



It would be even simpler to just allow underscores as a rule.



The expression comes down to this:



^(_[a-zA-Z0-9]+|[a-zA-Z0-9]+_?|[a-zA-Z0-9]+_[a-zA-Z0-9]+)$



^ First make sure we're matching from the beginning of the input.

( We have multiple options

_[a-zA-Z0-9]+ first find the underscore, followed by an unlimited number

of alphanumeric characters

| OR....

[a-zA-Z0-9]+_? find an unlimited number of alphanumeric characters followed

by an optional underscore

| OR....

[a-zA-Z0-9]+_[a-zA-Z0-9]+ find an unlimited numner of alphanumeric

characters followed by an underscore, followed by more characters.

) No more options

$ End of the string.



Now add a Regex validator to your page. Set the expression to the expression

above and the control to validate to your textbox and you're all done.



Jesse





-

Re:Usename regex

On Aug 1, 8:04 pm, Jesse Houwing <Jesse.houw...@nospam-sogeti.nl>

wrote:

Quote
Hello Mick,











>Hi All,



>I would like to know how I can limit users to only registering

>usernames

>which have alphanumberic characters and one underscore.

>From what I understand is I can use a regex to do this. But I have no

>idea how to do it. I have only used a customfieldvalidor before. And

>the

>reason I cant do it now is because of the design of the page itself.



>So basically I need to know how, upon a button click, I can compare

>the text entered in a textbox, to a regex, if its invalid throw a

>error, if its valid proceed.



>Thanks.



You can indeed use Regex for this. It's actually a simple expression:



because a regex describes the input from left to right you need to account

for all possible locations of the single '_' you're allowing.



It would be even simpler to just allow underscores as a rule.



The expression comes down to this:



^(_[a-zA-Z0-9]+|[a-zA-Z0-9]+_?|[a-zA-Z0-9]+_[a-zA-Z0-9]+)$



^ First make sure we're matching from the beginning of the input.

( We have multiple options

_[a-zA-Z0-9]+ first find the underscore, followed by an unlimited number

of alphanumeric characters

| OR....

[a-zA-Z0-9]+_? find an unlimited number of alphanumeric characters followed

by an optional underscore

| OR....

[a-zA-Z0-9]+_[a-zA-Z0-9]+ find an unlimited numner of alphanumeric

characters followed by an underscore, followed by more characters.

) No more options

$ End of the string.



Now add a Regex validator to your page. Set the expression to the expression

above and the control to validate to your textbox and you're all done.



Jesse- Hide quoted text -



- Show quoted text -



I just can add if the underscore is allowed in the middle of a word

only (I think which is usual for usernames), then expression can be as



^([a-zA-Z0-9]+_[a-zA-Z0-9]+)$



-