使用Formik反应验证最大范围

时间:2020-06-27 14:14:58

标签: javascript reactjs formik yup

我正在尝试使用React和Formik进行验证。我想实现最大位数仅为2,最大范围应仅为12。

    @SpringBootTest
    @AutoConfigureJdbc
    public class SampleTests {
        
            @Autowired
            BookRepository bookRepository;
        
            @Autowired
            AuthorRepository authorRepository;
        
            @Test
            public void createDeleteBook() {
        
                Author apj = new Author("APJ");
                apj = authorRepository.save(apj);
                Author arun = new Author("Arun Tiwari");
                arun = authorRepository.save(arun);
        
                Book book = new Book("Wings of Fire");
                book.addAuthor(apj);
                book.addAuthor(arun);
        
                bookRepository.save(book);
        
            }
        }

1 个答案:

答案 0 :(得分:0)

您可以使用此:

yup.string()
  .required("Select month")
  .min(2, "Invalid month format (Example: 06)")
  .max(2, "Invalid month format (Example: 06)")
  .matches(/^(0?[1-9]|1[012])$/, "Month must be greater than 0 and can't be exceeded 12");