What's the reason for not being able to include spaces as valid input to these methods?
How can I validate an alpha string that also includes spaces?
if you want to validate a string with alpha and space, we can use 'matches' method. The first param is your string and the second can be a RegExp object.
What's the point of using your validator then when I can just use JS-builtin RegExp?
A space character is not alpha or alphanumeric, that's why the validators don't allow it. We don't have validators for every possible combination of characters. You might need alpha + space while another user might need alphanumeric + space + apostrophe.
You're free to use regular expressions if that's all you need, but if you use the library you get:
I know it's not alpha or alphanumeric, there's really no point in explaining that. My point is why I can't configure additional characters in this and other methods just like you did for the credit card number method where you can add spaces that also validate.
Funny how you've just closed the thread before even considering adding this functionality when a user expresses a genuine interest in that...
When validating credit card numbers, we ignore spaces and hyphens, e.g. both 4012888888881881 and 4012 8888 8888 1881 are valid. Both are common formats and so this is not configurable.
Are you suggesting that we do the same with isAlpha and isAlphanumeric and allow something like foo bar 123 through? We can agree that a space character doesn't belong to either character group, and I don't think isAlpha(str, { allow_spaces: true }) or isAlpha(str, { extra_characters: ' ' }) make much sense. You're free to fork the library if you disagree.
Exactly that I'm suggesting. For example your other methodisCurrency(str [, options]) accepts the allow_space_after_symboloption which is great.
I'm validating tons of requests when I want a user to type e.g. a city name which can contain space(s) but must not contain any digits. I want them to be able to type "New York" and validate using the isAlpha method but with the option of accepting spaces as valid chars too.
Why not split by word and validate each separately?
input.split(' ').every(function (word) { return isAlpha(word); });
This has the added benefit of rejecting input with extra spaces:
> const valid = (input) => input.split(' ').every(function (str) { return isAlpha(str); })
undefined
> valid('New')
true
> valid('New York')
true
> valid('New York')
false
> valid('New York ')
false
Sure, I can do anything in vanilla JS, just like I did for my project because your validator wasn't usable in my case. I'm just signalling a missing option in your software, I'm not looking for programming advice.
Well thanks for the suggestion and sorry we couldn't be of more help.
No problems, thanks for your time and I wish you much success.
i subscribe to the isAlpha(str, { allow_spaces: true }) idea
Why not split by word and validate each separately?
input.split(' ').every(function (word) { return isAlpha(word); });This has the added benefit of rejecting input with extra spaces:
> const valid = (input) => input.split(' ').every(function (str) { return isAlpha(str); }) undefined > valid('New') true > valid('New York') true > valid('New York') false > valid('New York ') false
This solves my problem :) -Thank you
Hi, I'll just add my 馃憤 to also have this included.
Alpha+spaces, AlphaNum+spaces and AlphaDash/Hyphen are common enough that they should, IMHO, be actual separate rules.
Otherwise, any inputs for very common forms like "full name", "first and last name", "article title", "message subject", "article slug" and many other very common inputs for the most common forms will have to either be validated directly with regex or doing the split and validate each word individually which is pretty cumbersome.
As an example, see the rules from one of vuejs validation libraries (veevalidate) that has alpha, alpha_num, alpha_dash and alpha_space validators. Some other validation libs also include something similar.
Alternatively, some sort of "Alpha plus X" where we could, for example, add an array of allowed extra chars could also be useful and more flexible than a simple "allow_spaces: true".
Just my $0.02 but either way, thanks for making this lib, it's very useful.
I too came too was searching for this feature, but unfortunately I have to use regex. This was the only reason I installed this library, now this library isn't useful for my project.
Checkout this. https://www.npmjs.com/package/simple-react-validator#alpha_num
Allows for Validation with Alpha_num. Its React but shows implementation
input.split(' ').every(function (word) { return isAlpha(word); });
This doesn't work in express-validator for example where each function from validator.js is wrapped.
I used this in my middleware check and it worked magnifique. Posting here for anyone searching for an answer.
check('casting_verbal', 'Verbal requirements must be letters or numbers')
.optional()
.matches(/^[a-z0-9 ]+$/i)
found the answer on this closed issue:
https://github.com/validatorjs/validator.js/issues/305
I use const validator = require('validator'); to validate my alphanumeric names with spaces.
validate: {
validator: function (val) {
const valNoSpaces = val.split(' ').join('');
return validator.isAlpha(valNoSpaces, 'en-GB');
},
message: 'String have to contain only alphanumeric with spaces'
}
on schema mongoose is ok.
You can use split as suggested, but a better solution for me was using javascript's replace function to replace all special chars you want to accept in your validation
Support for Alpha and AlphaNumeric to contain options for 'contains spaces'. My use case is for checking an address Line 1
"George Henry, Moore Williams"
"Los Angeles"
"New York"
_The list goes on..._
That fact that I'd have to extra work to split the values doesn't make sense, when we could just pass a parameter (as proposed above) to allow spaces.
@AshHogarth @londonfox -- discussion resumed here - #1282
Most helpful comment
i subscribe to the
isAlpha(str, { allow_spaces: true })idea