rakit / validation

PHP Standalone Validation Library
MIT License
836 stars 143 forks source link

alpha_num (with spaces) #82

Open stevie-c91 opened 5 years ago

stevie-c91 commented 5 years ago

Hi, is there any support for a field which can have both alphabetical and numerical data, with spaces?

Thanks

jakewhiteley commented 5 years ago

@stevie-c91 This can be achieved pretty easily with the regex filter:

regex:/^[\s\d\pL\pM]+$/u will match any combinations of space characters, numbers, and letters from any language.

Alternatively just use regex:/^[\s\da-zA-Z]+$/ to match standard english alphabet characters.

You can test it at https://regex101.com/r/kwDmaD/3/

I will agree though, that it would be nice to have an alphanum filter or similar which can be easily used for username validating.

stevie-c91 commented 5 years ago

Thanks for the regex. alpha_num does exist and works nicely, but something like alpha_num_spaces would be nice for use in a 'message' textarea input.

emsifa commented 5 years ago

Hi @stevie-c91 .

You can create PR for it, or I will add this on my weekend.

Thanks.

jakewhiteley-gc commented 5 years ago

@emsifa I would suggest not alpha_num_spaces, but perhaps a filter for normal text input:

Maybe letter characters, space chars, numbers, and safe punctuation (-_;?! Etc). All those lower characters used in everyday typing - perhaps using the \p{P} flag?

I mean alpha_num_spaces isn't a bad idea, but then where does it stop?

I figured a rule for normal textarea input text, and a rule to match typical username characters (a-zA-Z0-9-_) would have a real world general use-case.

emsifa commented 5 years ago

@jakewhiteley Yes this would be endless, I'm about thinking of some options:

  1. Add a parameter for alpha_num and alpha rules to allows other characters. For example, if we want to allow comma, spaces, and dash, we can put alpha:, -, or alpha_num:, -.
  2. Add new rule called text (or maybe simple_text) that allows alpha num and space, if we want to allows other characters, we can put optional parameter like option 1, like text:\n ,-_ will allows a-z, 0-9, "\n" (line break), "," (comma), "-" (dash), and "" (underscore). We can set default allowed characters using static method like `Rakit\Validation\Rules\Text::setDefaultAllowedCharacters(" -,\n")`
  3. Add new rule called text with parameter type. Type can be something like textarea, markdown, slug, sentence, paragraph, etc. Each type has it's own allowed characters. By default it only allows a-z, 0-9, and space. And of course we can define custom type (maybe) using static method like Rakit\Validation\Rules\Text::defineType(string $type, string|array $chars).

With those options I think we are no longer need alpha_spaces and alpha_dash rules, and those will be flaged as deprecated.

Let me know what's your opinion, or maybe you have another idea.

jakewhiteley-gc commented 5 years ago

@emsifa that sounds ideal to me. Your solution adds some quick defaults which cover most every day situations.

Of course, for more complex situations (usernames for example) we still have the regex rule.

My only suggestion is to make sure not to use a-zA-Z in your rules, as these only really serves English speaking countries - the \pL\pM is much more useful as a match (and is used in the other text-based rules.

If you are doing the PR, tag me in it? I would love to see the new rules. Otherwise let me know if am to make the PR 😊

naveenparth commented 3 years ago

Have you added such feature for alpha-numeric with space character? if yes pleas let me know how to implement it. Thx.

leon486 commented 3 years ago

@emsifa that sounds ideal to me. Your solution adds some quick defaults which cover most every day situations.

Of course, for more complex situations (usernames for example) we still have the regex rule.

My only suggestion is to make sure not to use a-zA-Z in your rules, as these only really serves English speaking countries - the \pL\pM is much more useful as a match (and is used in the other text-based rules.

If you are doing the PR, tag me in it? I would love to see the new rules. Otherwise let me know if am to make the PR 😊

\pL and \pM don't seem to be working correctly with vee-validate for me.

jakewhiteley commented 3 years ago

@leon486 Thats because \p is a PHP regex special character: https://www.php.net/manual/en/regexp.reference.unicode.php

To do the same thing with js (client side) can be done by adding the u flag to your regex:

leon486 commented 3 years ago

@jakewhiteley Thanks for the clarification