uno
uno

Reputation: 1481

Client side form validation rails

Issue: My client side rails form input format validation won't work how the regex is set up to.

I have the following rails form input:

<%= form.text_field :twitter_link, 
    class: "form-control", 
    required: true, 
    pattern: "^(?:https?:\/\/)?(?:(?:www|m)\.)?twitter\.com\/\w+\/status\/\d+(?:\/\/?)?$", 
    title: "Twitter Comment URL" %>

When i enter the correct format, it won't validate and the "enter correct format" message appears.

The following regex in my model validates for the param:

/\A^(?:https?:\/\/)?(?:(?:www|m)\.)?twitter\.com\/\w+\/status\/\d+(?:\/\/?)?\z/

As you can see I have made a few changes from my model side validates to see if it would work. I have tried a few other things but nothing has solved.

Works fine for my model side validation but won't work client side. Is there something I need to change for it to work?

I am making a client side validation so I can have a custom message for the single text_field without the need for a flash or redirect. Any who, since I have a nested form 3 ways deep, I am having issues having the alert appear as a flash.now without needing to reload the page using a plain flash. Not sure what the issue is but this seems to be a good alternative.

How can I fix this so the regex works with the input text_field?

Upvotes: 1

Views: 726

Answers (1)

uno
uno

Reputation: 1481

Using a singly quotation instead of the double seems to do the trick.

<%= form.text_field :twitter_link, 
    class: "form-control", 
    required: true, 
    pattern: '^(?:https?:\/\/)?(?:(?:www|m)\.)?twitter\.com\/\w+\/status\/\d+(?:\/\/?)?$', 
    title: "Twitter Comment URL" %>

Not sure why i didn't try this before because i have ran into similar issues in the past with double vs single

Upvotes: 1

Related Questions