Giter Club home page Giter Club logo

Comments (5)

1000hz avatar 1000hz commented on June 30, 2024

No, since I'm piggy-backing off of HTML5 validation, there won't be any official support for textarea regex validation, but you can add a custom function validator that will run your regex like this:
#20 (comment)

In the next release, I'll probably add support into the API for custom validator functions, but this should solve your problem in the mean time.

from bootstrap-validator.

rudzikdawid avatar rudzikdawid commented on June 30, 2024

thanks for help. Now regex validation on textarea working like charm

js:

Validator.VALIDATORS.pattern = function ($el) {
        var patternRegExp = $el.data('pattern');
        return $el.val().match(eval(patternRegExp));
    }

html:

<textarea class="form-control" cols="20" data-error="error" data-pattern="/regex_here/g" data-pattern-error="error_text" name="name" placeholder="place_text" required="required" rows="5"></textarea>

from bootstrap-validator.

aryac avatar aryac commented on June 30, 2024

i'm beginner with bootstrap, need more help on this for textarea validation.

from bootstrap-validator.

adriennn avatar adriennn commented on June 30, 2024

@aryac what you can do is to edit the source of the plugin and add the error text for pattern matching:

  Validator.DEFAULTS = {
    delay: 500,
    html: false,
    disable: true,
    custom: {},
    errors: {
      match: 'Does not match',
      minlength: 'Not long enough',
      pattern: 'Illegal characters'
    }
  }

and then add the method (prefer new regExp() in Validator.VALIDATORS:

  Validator.VALIDATORS = {
    'native': function ($el) {
      var el = $el[0]
      return el.checkValidity ? el.checkValidity() : true
    },
    'match': function ($el) {
      var target = $el.data('match')
      return !$el.val() || $el.val() === $(target).val()
    },
    'minlength': function ($el) {
      var minlength = $el.data('minlength')
      return !$el.val() || $el.val().length >= minlength
    },
    'pattern': function ($el) {
      var pattern = $el.data('pattern')
      return !$el.val() || new RegExp(pattern,"g").test($el.val())
    }
  }

Then in you <textarea> add

data-pattern="your regex"

This works but it is not an encouragement to do user input filtering/sanitizing in the frontend.

from bootstrap-validator.

1000hz avatar 1000hz commented on June 30, 2024

You don't need to edit the source code of the plugin* since custom validators are now supported via the API. Pass the validator and error in as options when you initialize the validator.

$('#myForm').validator({
  custom: {
    pattern: function ($el) {
      var pattern = $el.data('pattern')
      return !$el.val() || new RegExp(pattern,"g").test($el.val())
    }
  },
  errors: {
    pattern: "Illegal characters"
  }
})

*Even before custom validators were fully supported, the preferred way to accomplish what you're suggesting would have been to add the extra validator and error at runtime inside of your own script instead of modifying the source of the plugin directly, since doing so would make future upgrades harder.

$.fn.validator.Constructor.VALIDATORS.pattern = function (e) {...}
$.fn.validator.Constructor.DEFAULTS.errors.pattern = "..."

from bootstrap-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.