admin管理员组文章数量:1388225
I'm new here. I have an issue with Yup validation. I want client enter number only from 0-9, without entering e, E, +, -
characters. I have code like this but user still can enter e, +, -. Is there any way to avoide these characters?
Yup.number()
.typeError("Please enter number value only")
.nullable()
.notRequired()
.min(0)
.max(100)
.moreThan(-1, "Negative values not accepted")
I try with string().matches(regex)
but it still showing err with negative values. Is there the best way to use .number()
without these characters?
I'm new here. I have an issue with Yup validation. I want client enter number only from 0-9, without entering e, E, +, -
characters. I have code like this but user still can enter e, +, -. Is there any way to avoide these characters?
Yup.number()
.typeError("Please enter number value only")
.nullable()
.notRequired()
.min(0)
.max(100)
.moreThan(-1, "Negative values not accepted")
I try with string().matches(regex)
but it still showing err with negative values. Is there the best way to use .number()
without these characters?
1 Answer
Reset to default 4You can add a custom validator with the test
method:
Yup
.number()
.nullable()
.notRequired()
.min(0)
.max(100)
.test(
"noEOrSign", // type of the validator (should be unique)
"Number had an 'e' or sign.", // error message
(value) => typeof value === "number" && !/[eE+-]/.test(value.toString())
);
本文标签: javascriptYup validate number onlyavoide ee charactersStack Overflow
版权声明:本文标题:javascript - Yup validate number only, avoide e, E, -, + characters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744529001a2610915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论