Code Monkey home page Code Monkey logo

Comments (5)

fiznool avatar fiznool commented on July 17, 2024

To implement this, I have added a new validator notBlank to my project:

Validator.add('notBlank', function(value, expectation) {
    return expectation === false || (_.isString(value) && !value.match(/^[\s\t\r\n]*$/));
}, "Can't be blank");

This matches all whitespace and newline characters. It can be used instead of required: true, as it implies that the string must be present.

I think this is enough to satisfy my use case and keeps required: true as-is, so I'll close this. However, how do you feel about a notBlank validator in the core? It seems like a reasonably common use case.

from backbone-validator.

fantactuka avatar fantactuka commented on July 17, 2024

@fiznool Reasonable validation. I've seen that some validators removes leading/trailing spaces before running validation, but it would be weird:

model.set("name", "     Joe    ", { validate: true });
// validation will run for trimmed value - "Joe", 
// but model will be set with white-spaced value "     Joe    "

So from my point your approach is way better. I'll see if there's a good way to have that as a part of core validators.
Do you think it verbose enough to have { blank: false }?

Thanks!

from backbone-validator.

fiznool avatar fiznool commented on July 17, 2024

I'd like to take credit, but all I've really done is 'borrow' the validation from node-validator...

Let me know if you'd like a pull request with the above validator, more than happy to help.

from backbone-validator.

fantactuka avatar fantactuka commented on July 17, 2024

Yeah, it would be great to have PR if possible. Make sure to add couple specs, should be simple: https://github.com/fantactuka/backbone-validator/blob/master/spec/backbone-validator-spec.js#L50

from backbone-validator.

fantactuka avatar fantactuka commented on July 17, 2024

FYI, v0.2.1 contains blank validator, that checks String, Array and Object for emptiness (string is trimmed before validation)

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