Use foundation to check text box length?

I use the base in a rails application, and I am looking for a way to check the length of text fields in a form: I would like to display an error when the text field contains too many characters (but not when it is empty).

I tried using Foundation abide and creating my own named templates, as described in docs .

Here is the contents of my application.js file, including custom templates during Foundation initialization:

 $(function(){ $(document) .foundation() .foundation('abide', { patterns: { short_field: /^.{,40}$/, long_field: /^.{,72}$/ } }); }); 

And here is my form code in the view:

 <form data-abide> <div class="long-name-field"> <input type="text" pattern="long_field" placeholder="Long Field"> <small class="error">Too long.</small> </div> <div class="short-name-field"> <input type="text" pattern="short_field" placeholder="Short Field"> <small class="error">Too long.</small> </div> </form> 

The problem is that when loading my form page, all the fields always display an error message, whether they are empty, filled out under their limit or exceed the limit of characters.

Anyone who has used successfully does something like this (or knows a better way that doesn't use custom templates)?

Greetings.

+4
source share
2 answers

I finally managed to get it to work!

The problem was that /^.{,40}$/ not a valid regexp syntax, you should use /^.{0,40}$/ .

I am mistaken with the syntax /.{5,}/ , which you can use to impose only a lower limit.

+5
source

I could not get javascript to work in my Rails 4 application, so I just added the regex directly as an attribute:

 <%= text_area_tag 'answer', @current_answer, :placeholder => 'required', :required => '', :pattern => '^(.){0,1000}$' %> 

To check the minimum length, I use only:

 <%= text_area_tag 'answer', @current_answer, :placeholder => 'required', :required => '', :pattern => '^(.){100,}$' %> 
0
source

Source: https://habr.com/ru/post/1499587/


All Articles