validation of special characters - ruby-on-rails

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.

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/

Use validates_format_of (see comments there for regex examples):
validates_format_of :username, :with => /\Ayour_regex_here\Z/i

Related

How to do a simple regex validation in rails

How can perform a reguler expression to validate for either - or _ in the person username. i dont want to accept any other character like .#()$etc just - or _ so the person can either have a name like mike, mikel_mark or mike-mark. very simple. Thank you
example:
validate_format_of :username, with: "...."
The Rails 3 way to do validations is the following:
validates :username, :format => {:with => /\A[0-9a-z_]+\Z/i}
The form of validate_format_of is more Rails < 3 like and followed the "type of validation" concept, whereas the validates form is attribute based (you write all validations that apply to the attribute in one statement).
Check out the docs here: http://apidock.com/rails/v3.2.13/ActiveModel/Validations/ClassMethods/validates

Negate regex of validates_format_of

In my rails model I have some kind of template system. I want to make sure that users editing it do not make accidental mistakes so I use some simple validators.
They can use markers like ##user_id## that will be replaced later. I want to make sure they do not enter something like ###user_id## that contains too many #, so not any ### (or ####) must appear in the field.
class Template
validates_format_of :text, :with => /##user_id##/,
:message => "##user_id## must be present"
validates_format_of :text, :not_with => /###/,
:message => "Too many #"
end
Unfortunately there is no :not_with option ... is there any chance to solve it using a :with-regex or should I go a separate validate method?
I tried some negative look-ahead, but as there are (mostly) several ## and only one ### they always match one of them.
Use the :without option:
validates_format_of :text, :without => /###/, :message => "Too many #"
What about this...
validates_format_of :text, :with => /(^|[^#])##user_id##($|[^#])/
EDIT: I copied acheong87's rubular examples with my regex.
Can you do something like this?
/^(.(?!###+user_id##|##user_id###+))*$/
Here's a live demo: http://rubular.com/r/SPwsyDlj0y.
In (more) English, it says,
A string in which no character is followed by ###+user_id## or ##user_id###+.

Rails - How to build a custom validation

I have a Rails 3 form, with data-remote => true.
The form field is handle, like a twitter handle. I want to add validation to ensure it's handle friendly, meaning, no spaces, or non-url friendly characters.
Where to start, do I build a customer validation for this?
Thanks
Looks like you want to use validates_format_of
class Product < ActiveRecord::Base
validates_format_of :handle, :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"
end
Change the regular expression pattern to match your needs. See the Rails Guides on validation for more information.
Look into validates_each
validates_each :handle do |record,attribute,value|
# your validation code here, and you can record.errors.add().
end
In rails 3 the best practice will be using
validates :handle, :format => {:with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"}

Not allowing space in password field when using authlogic

I am using authlogic for my user login functionality in my Rails app. But authlogic allows space in password and my application doesn't require this. So how can I do it?
Thanks!
You could use a regular expression to define your password rules.
validates_format_of :password, :with => /{regular expression here}/, :if => :require_password?, :message => "Must not contain any spaces!"

Passing a method to validates_format_of

I want to do this:
validates_format_of :email, :with => email_regex
def email_regex
email_name_regex = '[A-Z0-9_\.%\+\-]+'
domain_head_regex = '(?:[A-Z0-9\-]+\.)+'
domain_tld_regex = '(?:[A-Z]{2,4}|museum|travel)'
return /\A#{email_name_regex}##{domain_head_regex}#{domain_tld_regex}\z/i
end
but I am getting an error saying that I have to pass a regex to the validates method. I was sure youo could do something like this.
Am I missing a step?
Thanks
Your code looks OK. And I guess you define email_regex method before it is used by the validates method.
You have to debug what validates_format_of gets (modify RoR code to print the :with argument type). This should be quite easy and help you solve the problem.
Now I can only guess that email_regex method is redefined somewhere and returns something else than Regexp.
What about just using a contstant? This would also ensure that the regexp gets only compiled once.
class ...
FOO_BAR = /#{baz}.../
validatess ... :with => FOO_BAR
but yeah, I'm not sure why your way doesn't work.

Resources