最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use foundation abide to validate length of a text field? - Stack Overflow

programmeradmin0浏览0评论

I'm using foundation in a rails app and I'm looking for a way to validate the length of text fields in a form: I'd like to display an error when the text field contains too many characters (but not when it's empty).

I tried to use Foundation's abide and create custom named patterns as explained in the docs.

Here are the contents of my application.js file including the custom patterns upon 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 I load my form page all the fields always display the error message, whether they're empty, filled under their character limit or exceeding their character limit.

Anyone successfully used abide to do something similar (or knows a better way that is not using custom named patterns)?

Cheers.

I'm using foundation in a rails app and I'm looking for a way to validate the length of text fields in a form: I'd like to display an error when the text field contains too many characters (but not when it's empty).

I tried to use Foundation's abide and create custom named patterns as explained in the docs.

Here are the contents of my application.js file including the custom patterns upon 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 I load my form page all the fields always display the error message, whether they're empty, filled under their character limit or exceeding their character limit.

Anyone successfully used abide to do something similar (or knows a better way that is not using custom named patterns)?

Cheers.

Share Improve this question asked Aug 29, 2013 at 10:22 Daniel RisticDaniel Ristic 7106 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I finally managed to make it work!

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

I mistake it with the /.{5,}/ syntax that you can use to impose a only a lower limit.

I could not make the javascript abide work in my Rails 4 app, so I just added the regex directly as an attribute like so:

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

For validating minimum length only I use:

<%= text_area_tag 'answer', @current_answer,
        :placeholder => 'required', :required => '', :pattern => '^(.){100,}$' %>
发布评论

评论列表(0)

  1. 暂无评论