I would like to write custom validator like i`m trying below
ActiveRecord::Base.class_eval do
def self.validates_characters(*attr_names)
validates_each(attr_names) do |record, attr_name, value|
unless value.nil?
regex = /^[[a-zA-Z\][0-9]\]]\\$@^`,|%;.~()\/{}:?\[=\]\+\-_#!<&\s]*$/
record.errors.add(attr_name, 'invalid characters') unless regex.match(value)
end
end
end
end
I tried this regex => /^[[a-zA-Z\][0-9]\]]\\$@^`,|%;.~()\/{}:?\[=\]\+\-_#!<&\s]*$/ in rubular but it have some errors.
I would like to allow only for this characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1234567890
$@^`,|%;.~()/\{}:?[]=-+_#!<>& and spaces
So this string is not valid => " © gfdgfd 0543"
Is better way to do this??
If you only want to match those characters you could just use this: