import mongoose from "mongoose";
const mobileNoSchema = mongoose.Schema(
mobileNo: {
type: String,
// min: [10, "no is < 10"],
// max: [10, "no is > 10"],
validate: {
validator: function(v) {
if(v.length === 10) return true;
return /d{10}/.test(v);
},
message: props => `${props.value} is not a valid phone number!`
}
}
);
const Mobile= mongoose.model("mobile", mobileNoSchema);
export default Mobile;
My code is not working if i try using min
and max
validating only for max value
can anybody help me out
2
Answers
min
andmax
are validators for typeNumber
. They are used to compare Number values, not the lengths of the strings. If you want to compare lengths, useminLength
andmaxLength
validators. Also, if you need to check whether all characters are numbers usematch
validator like this:Documentation link.
Try with this condition below: