I am trying to write a regular expression in Ruby (Rails), so that username characters only contain numbers and letters (also no spaces).
I have this regular expression, /^[a-zA-Z0-9]+$/ , but it doesnβt seem to work, and I get an error message in Rails that says: "The provided regular expression uses multi-line anchors (^ or $), which could be a security risk. Did you mean to use \ A and \ z or forgot to add: multiline => true option? "
My complete code for this implementation in my user.rb model is:
class User < ActiveRecord::Base before_save { self.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_USERNAME_REGEX = /^[a-zA-Z0-9]+$/ validates :username, presence: true, length: { maximum: 20 }, format: { with: VALID_USERNAME_REGEX }, uniqueness: { case_sensitive: false } VALID_EMAIL_REGEX = /\A[\w+\-.] +@ [az\d\-.]+\.[az]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password, length: { minimum: 6 } end
What am I doing wrong and how can I fix this regular expression so that it is valid only for numbers and letters and spaces? Thanks.
source share