带有 ruby​​ on rails 形式的正则表达式模式 [英] regex pattern with form for ruby on rails

查看:41
本文介绍了带有 ruby​​ on rails 形式的正则表达式模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 form_for 时是否支持模式属性?

is there support for pattern attribute when using form_for?

<%= form_for order_form do |f| %>
<%= f.label "name" %>
<%= f.text_field :name, required: true %>
<%= f.label "Number" %>
<%= f.telephone_field :phone, pattern: "\\d{10}" %>
<%= f.submit %>

我正在尝试组合一个正则表达式模式以确保电话号码的格式正确.我的问题是模式属性不会只接受正则表达式

I'm trying to put together a regex pattern to ensure that phone numbers are in correct format. My problem is that the pattern attribute wont take regex only strings

<%= f.telephone_field :phone, pattern: /\d{10}/ %> doesnt work

所以当我把正则表达式写成一个字符串时,它会导致问题,(比如必须转义反斜杠,例如

so when i write the regex as a string it causes problems, (like having to escape backslashes e.g.

"\\d{10}" == /\d{10}/

我应该放弃在这个表单上使用 form_for 还是有办法将 form_for 和模式匹配一​​起使用

should i just forgo using form_for on this form or is there a way to use form_for and pattern matching together

推荐答案

我尝试了一些类似的方法,但我使用的是 Rails 5.1.4 我的代码是:

I was trying some similar but I work in Rails 5.1.4 My code is:

<%= form.telephone_field :telefono, id: :person_telefono,
:pattern => '\d{10}', :placeholder => "solo numeros"%>

尝试使其适应您的程序和 Rails 版本.

Try to adapte it for your program and rails version.

问候

这篇关于带有 ruby​​ on rails 形式的正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆