validation icon indicating copy to clipboard operation
validation copied to clipboard

alpha_num (with spaces)

Open stevie-c91 opened this issue 6 years ago • 10 comments

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

Thanks

stevie-c91 avatar Dec 19 '18 11:12 stevie-c91

@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.

jakewhiteley avatar Dec 19 '18 14:12 jakewhiteley

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.

stevie-c91 avatar Dec 19 '18 14:12 stevie-c91

Hi @stevie-c91 .

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

Thanks.

emsifa avatar Dec 21 '18 03:12 emsifa

@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.

jakewhiteley-gc avatar Dec 25 '18 17:12 jakewhiteley-gc

@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.

emsifa avatar Dec 26 '18 13:12 emsifa

@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 😊

jakewhiteley-gc avatar Dec 26 '18 18:12 jakewhiteley-gc

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

naveenparth avatar Jan 22 '21 07:01 naveenparth

@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.

leon486 avatar Feb 20 '21 17:02 leon486

@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:

  • https://javascript.info/regexp-unicode
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes

jakewhiteley avatar Feb 22 '21 13:02 jakewhiteley

@jakewhiteley Thanks for the clarification

leon486 avatar Feb 22 '21 14:02 leon486