如何解决在React中的RegEx中验证电话号码
我正在尝试在React (044) 456-7890
的regEx中验证这种电话号码。
在此处{@ 3}检查codesanbox
代码
const telRegExp = "(d{3})s*d{3}-d{4}";
let validateSchema = yup.object().shape({
tel_no: yup.string().matches(telRegExp,"Telephone number is invalid")
});
<InputMask
mask="(999) 999 - 9999"
onChange={handleChange}
onBlur={handleBlur}
>
{() => (
<TextField
label="Telephone Number (Ex: (044) 878 - 3900)"
name="tel_no"
fullWidth
variant="outlined"
helperText={touched.tel_no ? errors.tel_no : ""}
error={touched.tel_no && Boolean(errors.tel_no)}
/>
)}
</InputMask>
解决方法
要验证确切的数字格式#pragma
,我建议:
(044) 456-7890
Demo
,
^\(\d{3}\)\s\d{3}-\d{4}$
这与给定格式完全匹配
https://regex101.com/r/HBvG3K/6
\( and \) - To escape () characters and match literally
\s - space character. If there can be more or no spaces then add * next to \s
\d{} - decimal numbers(0-9) followed by quantifier.
let validateSchema = yup.object().shape({
tel_no: yup
.string()
.matches(
/^\(\d{3}\)\s\d{3}\s-\s\d{4}/g,"Telephone number is invalid"
)
});
,
这样的真实电话检查库版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。