首页 > 解决方案 > 有没有办法使用 express-validator 检查第一个字符是否是字母?

问题描述

case 'user_name': { return [ check( 'content.data.userName', '用户名需要至少一个字母' ) // .matches('(?=. [az])(?=. [0-9]) ') .exists() .trim() .bail() .isLength({ min: 6 }) .withMessage('用户名必须至少有6个字符') .isLowercase() .withMessage('必须全是小写字母')

case 'user_name': {
            return [
                check(
                    'content.data.userName',
                    'username need atleast one alphabet'
                )
                    // .matches('(?=.*[a-z])(?=.*[0-9])')
                    .exists()
                    .trim()
                    .bail()
                    .isLength({ min: 6 })
                    .withMessage('User Name must be atleast have 6 characters')
                    .isLowercase()
                    .withMessage('Must be all small letters')

标签: node.jsexpressexpress-validator

解决方案


This regular expression helps you to check the first character is alphabet or not.

^[a-zA-Z][\w\s-]+

第一个字符只能是 a-zA-Z

不允许使用“空格”和“连字符(-)”以外的特殊字符

.matches(/^[a-zA-Z][\w\s-]+/)

希望有效,谢谢!


推荐阅读