Validator.js: How to validate alphanumeric with spaces

Created on 9 Aug 2014  路  3Comments  路  Source: validatorjs/validator.js

Hello I'd like to know how can a I validate a String with spaces, shall I use the match function? Thanks in advance.

Most helpful comment

Here's another alternative?

if (!validator.isAlphanumeric(validator.blacklist(str, ' '))) {
  throw new Error('Only a-z,A-Z,0-9');
}

All 3 comments

You can use the matches function

validator.matches(/^[a-z0-9 ]+$/i);

Here's another alternative?

if (!validator.isAlphanumeric(validator.blacklist(str, ' '))) {
  throw new Error('Only a-z,A-Z,0-9');
}

isAlpha(str [, locale, options])
options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philfreo picture philfreo  路  3Comments

frontendmonster picture frontendmonster  路  4Comments

galki picture galki  路  3Comments

AnandChowdhary picture AnandChowdhary  路  3Comments

mfbx9da4 picture mfbx9da4  路  4Comments