mardi 24 février 2015

Rails 4 form validate regex (title, body)

trying to validate title and body of micropost form. I have been trying to figure this out for a day now. It's getting personal... :)


I am new to both Rails (1.5month) and regex (1day)


For title: I want to allow UTF-8 characters and spaces For body: I want to allow UTF-8 chars, spaces and punctuation.


http://rubular.com/ (ruby v 2.1.5) tells me this is going to be OK:



/[[:space:]]*[[:alpha:]]*/
/[[:alpha:]]*[[:space:]]*/


But when I try this:



validates :title, presence: true, length: { minimum: 10, maximum: 60 }, format: { with: /[[:alpha:]]*[[:space:]]*/, message: "only letters" }


It lets digits and chars like these +!%/=( to slip through.


The following fails too. It doesn't allow me spaces - at least it raises error when I include digits or other strange characters:



format: { with: /\A[[:alpha:]]*[[:space:]]*\z/, message: "only letters" }


I also tried to make something like this but that makes no difference, fails too:



REGEX = ........
format: { with: REGEX, message: "only letters" }


These fail too:



format: { with: /\A[\p{L}\ ]\z/
format: { with: /[\p{L}\ ]/


Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire