最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - yup - allow two digits after comma, minimum and maximum for a decimal - Stack Overflow

programmeradmin0浏览0评论
const validationSchema = yup.object({
        amount: yup.number().positive().min(5, 'minimum 5').max(10, 'maximum 10'),
    });

How is possible to add validation for decimal with two digits after ma?

const validationSchema = yup.object({
        amount: yup.number().positive().min(5, 'minimum 5').max(10, 'maximum 10'),
    });

How is possible to add validation for decimal with two digits after ma?

Share Improve this question asked Mar 29, 2022 at 9:41 AlexAlex 9,74030 gold badges107 silver badges166 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

solved like this:

let patternTwoDigisAfterComma = /^\d+(\.\d{0,2})?$/;
const monStringValidator = yup
  .number()
  .positive()
  .test(
    "is-decimal",
    "The amount should be a decimal with maximum two digits after ma",
    (val: any) => {
      if (val != undefined) {
        return patternTwoDigisAfterComma.test(val);
      }
      return true;
    }
  )
  .min(5, "minimum 5")
  .max(10, "maximum 10")
  .required("Is required");

const validationSchema = yup.object({
  amount: monStringValidator,
});
发布评论

评论列表(0)

  1. 暂无评论