简体   繁体   中英

Particular field should not only contains numeric value

我想写一个正则表达式与Rails中的Validator一起使用,条件是“特殊字段(xyz)不应该只包含数字值”。

> 'Your String'.match(/^[^\d]+$/).to_a ###["Your String"]
> 'Your 123 String'.match(/^[^\d]+$/).to_a  ### []

You could match the field value against /\\D+/ - that will match one or more non-digit characters. Be aware, though that spaces, periods, and other special characters are considered non-digit, so you may have to add to that, depending on any other special cases that you may need to test for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM