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

javascript - How to validate using yup to check string min length, max length and allow empty - Stack Overflow

programmeradmin1浏览0评论
firstName: yup
    .string()
    .test(
        'len',
        'can be empty or with string at least 2 characters and not more than 10',
        (val) => val != undefined && (val.length == 0 || (val.length >= 2 && val.length <= 10) )
    )

in this case length min 2 and max 10 works, but when is empty is marked with error i tried with val.length == 0

firstName: yup
    .string()
    .test(
        'len',
        'can be empty or with string at least 2 characters and not more than 10',
        (val) => val != undefined && (val.length == 0 || (val.length >= 2 && val.length <= 10) )
    )

in this case length min 2 and max 10 works, but when is empty is marked with error i tried with val.length == 0

Share Improve this question asked Dec 30, 2021 at 11:14 AlexAlex 9,74030 gold badges107 silver badges166 bronze badges 5
  • I guess if you want to allow the value to be empty (0) remove val.length ==0 from code. – Mohit Kushwaha Commented Dec 30, 2021 at 11:20
  • add required() after string, will fix the issue – Tigran Petrosyan Commented Dec 30, 2021 at 11:20
  • @TigranPetrosyan it mus to allow empty – Alex Commented Dec 30, 2021 at 11:22
  • 1 Could it be that val bees undefined if no text is entered? – Peter B Commented Dec 30, 2021 at 11:24
  • @PeterB yeah, that was the issue :) – Alex Commented Dec 30, 2021 at 11:27
Add a ment  | 

3 Answers 3

Reset to default 4

Hello you can do it like this

const yup = require("yup");
const firstName = "";

const schema = yup
  .string()
  .test(
    "len",
    "can be empty or with string at least 2 characters and not more than 10",
    (val) => {
      if (val === undefined) {
        return true;
      }
      return val.length === 0 || (val.length >= 2 && val.length <= 10);
    }
  );

schema
  .validate(firstName)
  .then((response) => console.log(response))
  .catch((err) => console.log(err));

fixed by separate undefined check

  firstName: yup
            .string()
            .test(
                'len',
                'can be empty or with string at least 2 characters and not more than 10',
                (val) => {
                    if (val == undefined) {
                        return true;
                    }
                    return  ((val.length == 0 || (val.length >= 2 && val.length <= 10)))
                }
            ),

Global Function,

export const minLengthValidation = (value: string | undefined, limit: number | undefined = 3) => {
  if (value && value.length < limit) {
    return false
  }

  return true
}

Invoked the function,

  nick_name: yup
    .string()
    .max(50, 'Nickname must be less than 50 characters!')
    .test('min-length', 'Nickname must be greater than  3 characters!', value => minLengthValidation(value))
    .required('Nickname is required.'),

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论