24

I am trying to get it so that the validator tells you "username must be alphanumeric". This is my code so far. I have confirmed that it validates at the correct time. The only problem is that no matter what I try, The RegexValidator still chucks the default error ("enter a valid value").

This is my code. I also tried it without the 'message=' in front, and it still said "enter a valid value", instead of "username must be alphanumeric"

user = CharField(
    max_length=30,required=True,
    validators=[
        RegexValidator('^[a-zA-Z0-9]*$',
            message='Username must be Alphanumeric'
        ),
    ]
)
Braiam
  • 1
  • 11
  • 47
  • 78
matts1
  • 857
  • 1
  • 9
  • 20

4 Answers4

42

How about adding the error code:

user = CharField(
    max_length=30,
    required=True,
    validators=[
        RegexValidator(
            regex='^[a-zA-Z0-9]*$',
            message='Username must be Alphanumeric',
            code='invalid_username'
        ),
    ]
)
Hieu Nguyen
  • 8,563
  • 2
  • 36
  • 43
  • It appears to work when the code is any non-None value. It works with an empty string and the integer 5... – matts1 Jul 21 '13 at 01:27
  • 2
    From a Django core dev: "I tried to see if the code could be changed so as to choose the more specific message. But it is not possible to determine which is the most specific. Sometimes the validator error message is more specific, sometimes the field one is. That's why I think you should use the code trick above." https://code.djangoproject.com/ticket/17051 I think it's somewhat reasonable too. – Hieu Nguyen Jul 21 '13 at 02:00
8

I was having trouble running a RegexValidator, too. But I was trying to raise the error by saving the model instance. It will not work this way! Only when using ModelForms the validators are called automatically.

In https://docs.djangoproject.com/en/dev/ref/validators/#how-validators-are-run

Note that validators will not be run automatically when you save a model, but if you are using a ModelForm, it will run your validators on any fields that are included in your form."

jrvidotti
  • 520
  • 1
  • 6
  • 12
0

Try passing the messsage as,

user = CharField(
    max_length=30,
    required=True,
    validators=[
        RegexValidator(
            regex=r'^[a-zA-Z0-9]*$',
            message=_('Username must be Alphanumeric'),
        ),
    ]
)
Viren Rajput
  • 5,426
  • 5
  • 30
  • 41
  • I assume the _ was a typo? This doesn't work - it seems like the same syntax, anyway. The the brackets don't do anything unless it really is a function (and I did try _ anyway, getting a NameError) – matts1 Jul 20 '13 at 09:16
  • @matts1, I don't think `_` is a typo. Look [here](https://docs.djangoproject.com/en/dev/topics/i18n/translation/). – awesoon Jul 20 '13 at 09:46
  • Unfortunately, although it made sense, it didn't fix the problem – matts1 Jul 20 '13 at 09:55
  • 1
    Nope `_` is not typo! Well there was a `bug` a bug while back, which seems to be fixed now! Related to your issue https://code.djangoproject.com/ticket/17051 – Viren Rajput Jul 20 '13 at 09:56
0
a validate user name here should contain at least one minuscule letter, one capital letter and one numeric, if i understand your code.
to complete Virendra Rajput answer correct the regex with that:
regex=r'^[a-zA-Z0-9]*$'   start with the r'
drabo2005
  • 1,076
  • 1
  • 10
  • 16
  • The regex is fine. Although admittedly the r is better practice to start with, it is only neccecary if you have backslashes in the regex. As stated in the question, it detects it correctly, but simply displays the wrong error. – matts1 Jul 20 '13 at 09:14
  • try to combine the user name with mini,capital et numeric together and see what happen – drabo2005 Jul 20 '13 at 09:24
  • A string is a string, no matter what its contents are. The contents of the string should not affect whether it outputs. – matts1 Jul 20 '13 at 09:28
  • that no the case with django for the authentication – drabo2005 Jul 20 '13 at 09:31
  • As I said, the regex is not the problem. It correctly detects when there is and isn't an error in the username. The problem is the output. – matts1 Jul 20 '13 at 10:20