tags:

views:

230

answers:

1
+1  Q: 

Django RegexField

Hello, at the registration process I want to enforce typing first and last name of the users. For this purpose, I need to write a regex.

What I need is the username should accept any char input but starting with a number like (1sdasd) or an empty string (like pressing space at this field which makes an empty string " ")

What can be the regular expression for it ?

ps: It should accept non-latin chars such as üÜğĞçÇöÖ (ie r'^\w+$', doesnt work)

Thanks

+1  A: 

You can compile a pattern with the unicode flag:

pattern = re.compile(r"^[^\W\d]\w*$", re.UNICODE)

The first character is matched by [^\W\d], which fails to match things that aren't word characters, and fails to match digits. Everything else is accepted (i.e. word characters that aren't digits). The rest can be any word characters including digits.

Mark Byers
thanks, what about the protection for entering number as the first character as I asked above? Thanks
Hellnar
Updated answer.
Mark Byers
now working like a charm, thanks!
Hellnar