How to use the isAlphanumeric function from validator

Find comprehensive JavaScript validator.isAlphanumeric code examples handpicked from public code repositorys.

validator.isAlphanumeric is a function that checks if a string contains only letters and numbers.

114
115
116
117
118
119
120
121
122
123
    translator(
      customMessage || 'alphanumeric',
      { paramName: paramName },
      locals
    ),
    validator.isAlphanumeric
  )
}

validations.isArray = function isArray(paramName, customMessage) {
fork icon9
star icon157
watch icon3

+ 5 other calls in file

26
27
28
29
30
31
32

console.log('ccc\n', 'isHexColor', validator.isHexColor('ccc'));
console.log('ggg0000\n', 'isHexColor', validator.isHexColor('ggg0000'));


console.log('ccc12312asdafas\n', 'isAlphanumeric', validator.isAlphanumeric('ccc12312asdafas'));
console.log('ggg-0000\n', 'isAlphanumeric', validator.isAlphanumeric('ggg-0000'));
fork icon15
star icon3
watch icon4

+ 3 other calls in file

How does validator.isAlphanumeric work?

validator.isAlphanumeric is a function in the validator library that checks whether a given string contains only letters and numbers. It uses a regular expression to match the string against alphanumeric characters. If the string contains only alphanumeric characters, the function returns true. Otherwise, it returns false.

62
63
64
65
66
67
68
69
70
71
  }
  return true;
},
isAlphanumeric: function(rule, value) {
  if (rule) {
    return validator.isAlphanumeric(value);
  }
  return true;
},
isBase64: function(rule, value) {
fork icon0
star icon2
watch icon2

+ 5 other calls in file

67
68
69
70
71
72
73
74
75
76
77
78
79
    axios.get("test.com/" + req.query.tainted); // OK. False Positive
  }
});


// safe validators
const validAlphanumeric = url => validator.isAlphanumeric(url);


const validAlpha = url => validator.isAlpha(url);


const validDecimal = url => validator.isDecimal(url);
fork icon5
star icon63
watch icon0

Ai Example

1
2
3
4
5
const validator = require("validator");

console.log(validator.isAlphanumeric("abc123")); // true
console.log(validator.isAlphanumeric("abc!123")); // false
console.log(validator.isAlphanumeric("")); // false

In this example, the validator module is imported and the isAlphanumeric() function is called with different strings as arguments. The function returns true if the string contains only letters and numbers, and false otherwise.

17
18
19
20
21
22
23
24
25
26
this.isAlpha = (subject) => {
    return StringValidator.isAlpha(subject) ? null:  subject + ' is not alpha';
}

this.isAlphanumeric = (subject) =>  {
    return StringValidator.isAlphanumeric(subject) ? null : subject + ' is not alphanumeric';
}

this.isAscii = (subject) => {
    return StringValidator.isAscii(subject) ? null : subject + ' is not ascii';
fork icon1
star icon4
watch icon2

+ 3 other calls in file

13
14
15
16
17
18
19
20
21
22
  console.log(this.count);
}

validateUsername(username, password, mobilephone, usertype) {
  return (
    validator.isAlphanumeric(username) &&
    /^((84|0[3|5|7|8|9])+([0-9]{8}))$/.test(mobilephone) &&
    validator.isIn(usertype, [ 1, 2]) &&
    validator.isLength(username, { min: 4, max: 32 }) &&
    validator.isLength(password, { min: 6, max: 32 })
fork icon0
star icon1
watch icon0

7
8
9
10
11
12
13
14
15
16
class CustomersController {
    // [POST]  /customers/register
    async register(req, res, next) {
        const data = req.body;
        if (
            validator.isAlphanumeric(data.userName) &&
            validator.isLength(data.userName, { min: 3, max: 16 }) &&
            validator.isStrongPassword(data.password) &&
            validator.isAlphanumeric(data.name) &&
            validator.isLength(data.name, { min: 3, max: 16 }) &&
fork icon1
star icon0
watch icon0

+ 2 other calls in file

32
33
34
35
36
37
38
39
40
if (this.data.password.length > 50) {this.errors.push("Password cannot exceed 50 characters.")}
if (this.data.username.length > 0 && this.data.username.length < 3) {this.errors.push("Username must be at least 3 characters.")}
if (this.data.username.length > 30) {this.errors.push("Username cannot exceed 30 characters.")}

// Only if username is valid then check to see if it's already taken
if (this.data.username.length > 2 && this.data.username.length < 31 && validator.isAlphanumeric(this.data.username)) {
  let usernameExists = await usersCollection.findOne({username: this.data.username})
  if (usernameExists) {this.errors.push("That username is already taken.")}
}
fork icon0
star icon0
watch icon1

58
59
60
61
62
63
64
65
66
67
}
// unique values
if (
  this.data.username.length > 2 &&
  this.data.username.length < 31 &&
  validator.isAlphanumeric(this.data.username)
) {
  let usernameExists = await usersCollection.findOne({
    username: this.data.username,
  });
fork icon0
star icon0
watch icon1

116
117
118
119
120
121
122
123
124
125

// Only if username and email is valid then check to see if it's already taken
if (
  this.data.user_name.length > 2 &&
  this.data.user_name.length < 31 &&
  validator.isAlphanumeric(this.data.user_name)
) {
  checkExistUsername().then((result) => {
    if (result.length) {
      result.forEach((element) => {
fork icon0
star icon0
watch icon1

63
64
65
66
67
68
69
70
71
72
if (text == undefined) {
  return ['text: key not found'];
} else {
  text = String(text);
}
return validator.isAlphanumeric(text, 'de-DE', {
  ignore: '+-_.:,;&%*()[]{}\'"#!? ',
})
  ? 0
  : [
fork icon0
star icon0
watch icon1

+ 11 other calls in file

34
35
36
37
38
39
40
41
42
if (this.data.password.length > 0 && this.data.password.length < 6) {this.errors.push("Password must be at least 6 characters.")}
if (this.data.password.length > 50) {this.errors.push("Password cannot exceed 50 characters.")}
if (this.data.username.length > 0 && this.data.username.length < 3) {this.errors.push("Username must be at least 3 characters.")}
if (this.data.username.length > 30) {this.errors.push("Username cannot exceed 30 characters.")}

if(this.data.username.length > 2 && this.data.username.length < 31 && validator.isAlphanumeric(this.data.username)){
    let usernameExist = await usersCollection.findOne({username: this.data.username})
    if(usernameExist) {this.errors.push("Username already exists.")}
}
fork icon0
star icon0
watch icon1

136
137
138
139
140
141
142
143
144
145
    letterSpacing,
    lineHeight,
},
allowreplay: config.options.challengeButton || (validator.isBoolean(`${req.headers.allowreplay}`) ? parseBool(req.headers.allowreplay) : false),
allowreauth: config.options.allowreauth || false,
mrhsession: validator.isAlphanumeric(`${req.headers.mrhsession}`) && req.headers.mrhsession ? req.headers.mrhsession : 'none',
serverlog: {
    client: config.serverlog.client || false,
    server: config.serverlog.server || false,
},
fork icon0
star icon0
watch icon0

44
45
46
47
48
49
50
51
52
53
function validatorType (type) {
  switch (type) {
    case 'int':
      return validator.isInt;
    case 'safe_string':
      return validator.isAlphanumeric;
    case 'boolean':
      return validator.isBoolean;
    case 'string':
      return function (value) {
fork icon0
star icon0
watch icon0

32
33
34
35
36
37
38
39
40
41
42
43
app.post('/login', (req, res) => {
  let email = req.body.email
  let user_password = req.body.user_password


  let is_email_OK = validator.isEmail(email)
  let is_user_password_OK = validator.isAlphanumeric(user_password, ['pt-BR'], {
    ignore: acceptableCharacters.inPassword
  })


  if (is_email_OK && is_user_password_OK) {
fork icon0
star icon0
watch icon0

+ 9 other calls in file

163
164
165
166
167
168
169
170
171
172
return validator(username)
  .custom((paramUsername) => {
    if (
      validatorLibrary.isEmail(paramUsername) ||
      validatorLibrary.isMobilePhone(paramUsername) ||
      validatorLibrary.isAlphanumeric(paramUsername, 'en-US', { ignore: '.' })
    ) {
      return true;
    }
    throw false;
fork icon0
star icon0
watch icon0