Giter Club home page Giter Club logo

Comments (6)

dkiestra avatar dkiestra commented on May 22, 2024

I just realize that the match validation rule can be used for exactly that purpose.

from jquery-validator.

dkiestra avatar dkiestra commented on May 22, 2024

I tried to make a custom validation for a time (hh:mm) value. The match:/^([01]\d|2[0-3]):?([0-5]\d)$/ string should do the trick. However, it contains the alternation regex character (pipe symbol) that can't be used in the match validation rules because it is already used for separating the validation rules. When I try to use the above rule it is split at the | character and javascript crashes due to an unmatched parenthesis. Is there any solution for this?

from jquery-validator.

dkiestra avatar dkiestra commented on May 22, 2024

Also, it is not possible to use a colon (the : character) in the regular expression because it is already used for separating the rule name from a given value.

from jquery-validator.

joecwallace avatar joecwallace commented on May 22, 2024

I think I'll probably add custom validation rules so that you don't have to rely on the match/regex rule.

$(document).ready(function() {
    $('#myForm').validator({
      events   : 'submit',
      selector : 'input[type!=submit], select, textarea',
      callback : function( elem, valid ) {
          if ( ! valid ) {
              $( elem ).addClass('error');
          }
      },
      validate_time: function(attribute, value, parameters) {
        return /^([01]\d|2[0-3]):?([0-5]\d)$/.match(value);
      }
    });
});

And of course, the markup:

<input type="text" data-validations="time">

What do you think about that?

from jquery-validator.

dkiestra avatar dkiestra commented on May 22, 2024

That would be great. But what about the Laravel validation rules? I suppose the server and client side validation should match as much as possible. Can I make suggestions for the Laravel validation rules too?

from jquery-validator.

joecwallace avatar joecwallace commented on May 22, 2024

I'm not sure what you mean. You're obviously not going to be able to completely reuse code - this plugin isn't going to execute arbitrary PHP - but you could reuse your regular expression.

For example:

Validator::register('awesome', function($attribute, $value, $parameters)
{
    return $value == Config::get('validation.awesome');
});

And in your template (using Blade):

$('...').validator({
    ...
    validate_awesome: function (attribute, value, parameters) {
        return value == {{ Config::get('validation.awesome') }};
    }
});

Obviously, that's just a string comparison, but it would be easy to modify for regular expressions.

Anyway, I added the custom validation rules, so you should be able to do things like I mentioned in the comment above.

from jquery-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.