views:

54

answers:

2

i want to put validation in ruby on rails that my username should not contain special characters like !@#$%^*()_-+=/<>?:'";. Please tell me how can i implement it in my code.

A: 

Use validates_format_of (see comments there for regex examples):

validates_format_of :username, :with => /\Ayour_regex_here\Z/i
Voyta
+2  A: 

Using your model validates_format_of as suggested by @Voyta

I am adding a regular expression in my example though :

validates_format_of :username, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/
Francisco Soto