Ruby-on-rails 验证:仅字母、数字和 -

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1136049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:28:28  来源:igfitidea点击:

Validate: Only letters, numbers and -

ruby-on-railsrubyregex

提问by atmorell

I would like to validate my users, so they can only use a-z and - in their username.

我想验证我的用户,所以他们只能在他们的用户名中使用 az 和 - 。

validates_format_of :username, :with => /[a-z]/

However this rule also allows spaces ._@

但是这个规则也允许空格 ._@

Username should use only letters, numbers, spaces, and .-_@ please.

Any ideas?

有任何想法吗?

Best regards. Asbj?rn Morell

此致。Asbj?rn 莫雷尔

回答by Matthew Schinckel

You may need to say the whole string must match:

您可能需要说整个字符串必须匹配:

validates_format_of :username, :with => /^[-a-z]+$/

You may also need to replace ^ with \A and $ with \Z, if you don't want to match a newline at the start/end. (thanks to BaroqueBobcat)

如果您不想在开始/结束处匹配换行符,您可能还需要将 ^ 替换为 \A 并将 $ 替换为 \Z。(感谢BaroqueBobcat

Appending an i will cause it to match in a case-insensitive manner. (thanks to Omar Qureshi).

附加 i 将使其以不区分大小写的方式匹配。(感谢奥马尔·库雷希)。

(I also originally left off the +: thanks to Chuck)

(我最初也离开了+:感谢Chuck

回答by laffuste

More complex solution but reusable and with more fine grained error messaging.

更复杂的解决方案,但可重用且具有更细粒度的错误消息。

Custom validator:

自定义验证器:

app/validators/username_convention_validator.rb

应用程序/验证器/用户名_convention_validator.rb

class UsernameConventionValidator < ActiveModel::EachValidator
  def validate_each(record, field, value)
    unless value.blank?
      record.errors[field] << "is not alphanumeric (letters, numbers, underscores or periods)" unless value =~ /^[[:alnum:]._-]+$/
      record.errors[field] << "should start with a letter" unless value[0] =~ /[A-Za-z]/
      record.errors[field] << "contains illegal characters" unless value.ascii_only?
    end
  end
end

(Notice it does allow ' . - _ ' and doesnt allow non ascii, for completeness sake)

(注意它确实允许 ' . - _ ' 并且不允许非 ascii,为了完整起见)

Usage:

用法:

app/models/user.rb

应用程序/模型/user.rb

validates :name,
    :presence => true,
    :uniqueness => true,
    :username_convention => true

回答by djna

The [] may contain several "rules" so [a-z0-9] gives lowercase letters and numbers

[] 可能包含多个“规则”,因此 [a-z0-9] 给出小写字母和数字

the special character - must go at the start of the rule

特殊字符 - 必须放在规则的开头

Does

[-a-z0-9@_.] 

give the effect you want?

给你想要的效果?

回答by Codebeef

validates_format_of :username, :with => /^[\w\-@]*$/

Note the *, which means '0 or more'

注意 *,表示“0 或更多”

回答by Damir Zeki?

Simply change the regular expression to match all characters your specification states (\wcovers all alphanumeric characters -- letters and numbers -- and an underscore).

只需更改正则表达式以匹配您的规范说明的所有字符(\w涵盖所有字母数字字符——字母和数字——以及下划线)。

validates_format_of :username, :with => /[\w \.\-@]+/