Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Validator for Alphanumeric

I am trying to only allow alphanumeric fields for a first name using:

patientFirstName:
                 - NotBlank: ~
                 - Regex: 
                       pattern: "/[^a-z_\-0-9]/i"
                       htmlPattern: "^[a-zA-Z\-0-9]+$"
                       message: Name must be alphanumeric

However it is still allowing characters like "&&&". Is my regex wrong?

like image 581
kratos Avatar asked Nov 26 '13 22:11

kratos


2 Answers

Symfony 3

If you do not want to use regex, you can use @Assert\Type for validator alphanumeric

You can see it in the symfony doc here

@Assert\Type(type="alnum")
like image 173
Shigiang Liu Avatar answered Nov 09 '22 15:11

Shigiang Liu


You are missing ^, $ and + (just like your html pattern): /^[a-z\-0-9]+$/i

If you do not add them, the regex will match any string if it contains at least one alphanumeric character.

like image 5
Wouter J Avatar answered Nov 09 '22 15:11

Wouter J