简体   繁体   中英

How to validate the length of character is equals to 5 using express-validator?

I am planning to make partnerid field character length as 5 which means user will get error message if he enters less than 5 character or more than 5 characters which will include alpha-numeric character. How can we do it using express-validator? I tried using below code but it didn't worked Thanks

   req.checkBody('partnerid', 'Partnerid field must be 5 character long ').len(5);

您可以使用express-validator isLength()选项来检查5 maxmin长度:

 req.checkBody('partnerid', 'Partnerid field must be 5 character long ').isLength({ min: 5, max:5 });

您可以使用express-validator matches选项来检查伙伴字段是否仅包含字母数字并且长度为5

req.checkBody('partnerid', 'Partnerid field must be 5 character long ').matches(/^[a-zA-Z0-9]{5}$/, "i");

.len(5) not support in express validation, you can use .isLength(5) for check max and min length is 5.

req.checkBody('partnerid', 'Partnerid field must be 5 character long strong text').isLength(5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM