How To Check MongoDB ID Valid Or Not
As you must aware about the object ID in mongo database or MongoDB is 24 characters long so for check is it valid id or not there is no direct validation method in validations package such as node-input-validator.
MongoDB - Education Funda |
So for checking entered id is valid or not we can us below tech-tics:
Approach 1 - Simple JS Validation
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$")
checkForHexRegExp.test("i am a string") // false
checkForHexRegExp.test("5e63c3a5e4232e4cd0074ac2") // true
Approach 2 - With Mongo DB Method (Best Way)
var ObjectID = require("mongodb").ObjectID
ObjectID.isValid("i am a string") // false
ObjectID.isValid("5e63c3a5e4232e4cd0274332") // true
With Node Input Validator You Can Use Below Method:
Thanks for reading this blog,I hope it will helpful for you.
Comments
Post a Comment