guillaumepotier / Parsley.js

Validate your forms, frontend, without writing a single line of javascript
http://parsleyjs.org
MIT License
9.04k stars 1.31k forks source link

Some useful validators #1288

Closed Marco-Sulla closed 5 years ago

Marco-Sulla commented 5 years ago

I think these are a good set of custom validator. I hope they could be added (with some improvement):

window.Parsley.addValidator('date', {
    validate: function(value, format) {
        var date = moment(value, format, true);
        return date.isValid();
    },
    messages: {
        en: 'It must be a valid date with format %s',
        it: 'Deve essere una data valida nel formato %s'
    }
});

window.Parsley.addValidator('vatcode', {
    validate: function(value, format, parsleyField) {
        var re = /^[A-Za-z][A-Za-z][A-Za-z0-9]{1,13}$/;

        return re.test(value);
    },
    messages: {
        en: "It's not a valid VAT code",
        it: "Non è un codice IVA valido"
    }
});

window.Parsley.addValidator('phone', {
    validate: function(value, format, parsleyField) {
        var re = /^[0-9+*#,;]+$/;

        return re.test(value);
    },
    messages: {
        en: "Enter a valid phone number",
        it: "Immetti un numero telefonico valido"
    }
});
marcandre commented 5 years ago

Thanks for the PR. It's really more like 3 PRs too :-)

Bug: Your regex seem anchored only at the beginning (^) but not at the end (missing $).

1) date: Good, but depends on the moment library. I would rather not add another dependency, but maybe we could put it in extra/ or in the wiki

2) VAT: Sounds good. Two things though: should be stricter (can only be 2 letters + 13 letters maximum) and requires tests. It would be awesome if you could make a separate PR for this.

3) Phone number: I'm not sure everyone will agree on what can be part of a phone number or not. Note that HTML5 has a "tel" input type but has no validation for it.

Marco-Sulla commented 5 years ago

Bug: thank you I'll fix it

  1. ok for the 2+13, but validation is very difficult, since it depends by state. See Wikipedia for an example.

  2. If you see the check is very permissive. It allows any character that you can input in your smartphone, without checking any starting number or so on. I know that input='tel' has no validation, but this way it's practically useless.

marcandre commented 5 years ago

Bug: thank you I'll fix it

  1. ok for the 2+13, but validation is very difficult, since it depends by state. See Wikipedia for an example.

Right, you don't have to go state by state though, I would be afraid this could change in the future? But 2+13 shouldn't.

  1. If you see the check is very permissive. It allows any character that you can input in your smartphone, without checking any starting number or so on. I know that input='tel' has no validation, but this way it's practically useless.

Right, but it's probably too context dependant. For example we use () all the time in America for regional code, not so much in Europe. I don't want to provide one builtin.

Marco-Sulla commented 5 years ago

Ok, I updated the vatcode validator. Should I add a separate issue for date and vatcode validators?

marcandre commented 5 years ago

A PR for the vatcode validator would be great. Maybe vatin would be a better name, as it seems to be an accepted accronym for it?

Normally a different PR for the date validator in /extra would be best, but you can include them in the same PR if you prefer.

marcandre commented 5 years ago

BTW, it's {2,13}, not {1,13}

Marco-Sulla commented 5 years ago

Ok for vatin. Why 2?

marcandre commented 5 years ago

From wikipedia: "... and then has between 2 and 13 characters"

Marco-Sulla commented 5 years ago

Don't know, I read the list and the shortest are Romania (that is incorrect) and San Marino that seems to have the shortest: SM + 5 digits (source)

marcandre commented 5 years ago

Your link for thas is incorrect leads nowhere. What is Romania's shortest VATIN then? Might as well fix wikipedia...

Marco-Sulla commented 5 years ago

Excuse me, I fixed the link

Marco-Sulla commented 5 years ago

There's a little problem. Some countries uses spaces, hypens or dots as separators. Should we add these characters to the validator?

marcandre commented 5 years ago

Your link says 8. This link says 10. This other link says 2-10... Hard to say which is right 😢 . I posted on the Talk page of wikipedia, but for now probably best to keep it safe at minimum 2.

Marco-Sulla commented 5 years ago

Oh well, let be 2. What about special characters? PS: Indonesia seems to have a longer length, 20 characters, thanks to separators

marcandre commented 5 years ago

I was looking at EU VAT. Maybe the name should be EUVATIN? Looks like poland allows - for formatting, but I'd be fine with excluding it, your call.

Marco-Sulla commented 5 years ago

We can create two validators, vatin and euvatin. I think to let people to add the special characters, they could think it's not correct without them.

PS: it seems that Indonesia has a greater length. I think we can remove the max limit, since it can change in any state at any time.