regex pattern with form for ruby on rails

大憨熊 提交于 2020-12-06 02:39:23

问题


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}/

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


回答1:


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"%>

Try to adapte it for your program and rails version.

Regards



来源:https://stackoverflow.com/questions/41111555/regex-pattern-with-form-for-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!