1

A pattern argument can be provided to a form field as described here

Example (regex from here)

<%= f.text_field :username, pattern: "([A-Za-z0-9\-\_]+)" %>

When the regex is not adhered to, a message is displayed. E.g.:

enter image description here

How can that message be customised?

1 Answer 1

2

The pattern attribute is actually a HTML spec, not a Rails thing. You can use the title attribute to give users a hint on to what format is expected.

<%= f.text_field :username, pattern: "([A-Za-z0-9\-\_]+)",
                            title: "A username can only contain letters, numbers, hyphens and underscores" %>

Find more information here: https://html.spec.whatwg.org/multipage/input.html#the-pattern-attribute

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining that, I really couldn't figure out why pattern seemed to be missing from form_for rails guides/docs. I will know for next time now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.