用 Yup 验证 phone 号码?
Validate phone number with Yup?
我正在尝试用 Yup 验证 phone 号码:
phone: Yup.number()
.typeError("That doesn't look like a phone number")
.positive("A phone number can't start with a minus")
.integer("A phone number can't include a decimal point")
.min(8)
.required('A phone number is required'),
.min(8)
验证数字为 8 或更多。因此只需输入 8
即可通过。我怎样才能让 8 个字符成为必需的,这样 1000 0000
就会通过?
你好,我现在正在解决和你一样的问题,我找到了可能的解决方案。
使用匹配正则表达式的字符串验证 phone 数字
const phoneRegExp = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/
phoneNumber: Yup.string().matches(phoneRegExp, 'Phone number is not valid')
您可以搜索不同的正则表达式并验证它。
我使用了本文 https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204
中的正则表达式
试试这个,它可能对你有帮助。
手机:Yup.string().matches(/^[6-9]\d{9}$/, {消息:"Please enter valid number.", excludeEmptyString: false} )
>。更新。<
我创建了一个使用 google-libphonenumber
的 yup-phone
模块,它提供准确的验证检查,可以直接从 github
安装
npm install --save yup yup-phone
.
勾选Usage
const Yup = require('yup');
require('yup-phone');
// validate any phone number (defaults to India for country)
const phoneSchema = Yup.string().phone().required();
phoneSchema.isValid('9876543210'); // → true
phone 号码验证的正则表达式是
/^(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)?$/
例子
// index.js
const yup = require('yup');
const { rePhoneNumber } = require('./yup-phone')
const schema = yup.string().phone()
const phone = '+911234567890';
console.log('Is Valid? ', rePhoneNumber.test(phone)); // Is Valid? true
schema.validateSync(phone);
// yup-phone.js
const yup = require('yup');
const rePhoneNumber = /^(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)?$/;
module.exports.rePhoneNumber = rePhoneNumber
yup.addMethod(yup.string, "phone", function() {
return this.test("phone", "Phone number is not valid", value =>
rePhoneNumber.test(value)
);
});
只是一点点合作。就我而言,我不想验证输入是否为空(不需要时)。谢谢大家的例子!
yup.addMethod(yup.string, "phone", function(messageError = 'Phone number is not valid') {
const phoneRegExp = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/
return this.test('phone', messageError, value => {
if (value && value.length > 0) {
return phoneRegExp.test(value)
}
return true
})
})
我有一个类似的用例,这是我的解决方案:
// Regex source: https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s02.html
const phoneRegex = RegExp(
/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
);
const schema = yup.object().shape({
phone: yup.string().matches(phoneRegex, "Invalid phone").required("Phone is required")
});
const phoneRegExp = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/
phone_number: Yup.string()
.required("required")
.matches(phoneRegExp, 'Phone number is not valid')
.min(10, "to short")
.max(10, "to long"),
这最适合我...您可以设置自己的长度...我只想要 10 位数不多不少
我为此制作了一个名为 yup-phone-lite 的新包。我使用了原始的 yup-phone 包,但它使用了大量的 google-libphonenumber,所以我用一个更小的 fork 替换了它。
这是我的代码片段。
let schema = yup.object().shape({
phone: yup
.string()
// regexr.com/6anqd
.matches(/(\+91\ )[6-9]{1}[0-9 ]{4}[0-9 ]{4}[0-9]{3}/, {
message: "Invalid Indian number",
excludeEmptyString: false,
})
.required(),
email: yup.string().required().email(),
password: yup.string().required().min(8),
confirmPassword: yup
.string()
.required()
.oneOf([yup.ref("password")], "Passwords do not match"),
agree: yup
.boolean()
.oneOf(
[true],
"It is essential to accept our Privacy Policy to register.",
),
});
My regex 按照格式检查印度 Phone 数字,+91 axxx xxx xxx
(其中 a
可以是 6 到 9 范围内的数字,x
可以是 0 - 9).
我尝试了 yup-home 但它无法真正验证我想要的印度号码的范围,另外,包导入大小很昂贵。
此外,我在前端使用 React 和 Formik 进行表单验证 react-phone-input-2
<PhoneInput
country={"in"}
value={values.phone}
name="phone"
onChange={(phone, data, e) => handleChange(e)}
onBlur={(e) => handleBlur(e)}
defaultMask=".... ... ..."
masks={{ in: ".... ... ..." }}
onlyCountries={["in"]}
inputProps={{
name: "phone",
required: true,
autoFocus: true,
}}
disableSearchIcon={true}
disableDropdown={true}
containerClass="mt-1 border h-12 text-sm focus:outline-none block w-full bg-gray-100 dark:bg-transparent border-transparent focus:bg-white"
inputClass="mt-1 border h-12 text-sm focus:outline-none block w-full bg-gray-100 dark:bg-transparent border-transparent focus:bg-white"
inputStyle={{
background: "transparent",
border: "1px solid grey",
height: "3em",
width: "100%",
outline: "none",
}}
buttonStyle={{
height: "3em",
background: "transparent",
outline: "none",
border: "none",
}}
/>
{handleFormikError(errors, touched, "phone")} // returns the span.alert with error message
我正在尝试用 Yup 验证 phone 号码:
phone: Yup.number()
.typeError("That doesn't look like a phone number")
.positive("A phone number can't start with a minus")
.integer("A phone number can't include a decimal point")
.min(8)
.required('A phone number is required'),
.min(8)
验证数字为 8 或更多。因此只需输入 8
即可通过。我怎样才能让 8 个字符成为必需的,这样 1000 0000
就会通过?
你好,我现在正在解决和你一样的问题,我找到了可能的解决方案。
使用匹配正则表达式的字符串验证 phone 数字
const phoneRegExp = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/
phoneNumber: Yup.string().matches(phoneRegExp, 'Phone number is not valid')
您可以搜索不同的正则表达式并验证它。 我使用了本文 https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204
中的正则表达式试试这个,它可能对你有帮助。
手机:Yup.string().matches(/^[6-9]\d{9}$/, {消息:"Please enter valid number.", excludeEmptyString: false} )
>。更新。<
我创建了一个使用 google-libphonenumber
的 yup-phone
模块,它提供准确的验证检查,可以直接从 github
npm install --save yup yup-phone
.
勾选Usage
const Yup = require('yup');
require('yup-phone');
// validate any phone number (defaults to India for country)
const phoneSchema = Yup.string().phone().required();
phoneSchema.isValid('9876543210'); // → true
phone 号码验证的正则表达式是
/^(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)?$/
例子
// index.js
const yup = require('yup');
const { rePhoneNumber } = require('./yup-phone')
const schema = yup.string().phone()
const phone = '+911234567890';
console.log('Is Valid? ', rePhoneNumber.test(phone)); // Is Valid? true
schema.validateSync(phone);
// yup-phone.js
const yup = require('yup');
const rePhoneNumber = /^(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)?$/;
module.exports.rePhoneNumber = rePhoneNumber
yup.addMethod(yup.string, "phone", function() {
return this.test("phone", "Phone number is not valid", value =>
rePhoneNumber.test(value)
);
});
只是一点点合作。就我而言,我不想验证输入是否为空(不需要时)。谢谢大家的例子!
yup.addMethod(yup.string, "phone", function(messageError = 'Phone number is not valid') {
const phoneRegExp = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/
return this.test('phone', messageError, value => {
if (value && value.length > 0) {
return phoneRegExp.test(value)
}
return true
})
})
我有一个类似的用例,这是我的解决方案:
// Regex source: https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s02.html
const phoneRegex = RegExp(
/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
);
const schema = yup.object().shape({
phone: yup.string().matches(phoneRegex, "Invalid phone").required("Phone is required")
});
const phoneRegExp = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/
phone_number: Yup.string()
.required("required")
.matches(phoneRegExp, 'Phone number is not valid')
.min(10, "to short")
.max(10, "to long"),
这最适合我...您可以设置自己的长度...我只想要 10 位数不多不少
我为此制作了一个名为 yup-phone-lite 的新包。我使用了原始的 yup-phone 包,但它使用了大量的 google-libphonenumber,所以我用一个更小的 fork 替换了它。
这是我的代码片段。
let schema = yup.object().shape({
phone: yup
.string()
// regexr.com/6anqd
.matches(/(\+91\ )[6-9]{1}[0-9 ]{4}[0-9 ]{4}[0-9]{3}/, {
message: "Invalid Indian number",
excludeEmptyString: false,
})
.required(),
email: yup.string().required().email(),
password: yup.string().required().min(8),
confirmPassword: yup
.string()
.required()
.oneOf([yup.ref("password")], "Passwords do not match"),
agree: yup
.boolean()
.oneOf(
[true],
"It is essential to accept our Privacy Policy to register.",
),
});
My regex 按照格式检查印度 Phone 数字,+91 axxx xxx xxx
(其中 a
可以是 6 到 9 范围内的数字,x
可以是 0 - 9).
我尝试了 yup-home 但它无法真正验证我想要的印度号码的范围,另外,包导入大小很昂贵。
此外,我在前端使用 React 和 Formik 进行表单验证 react-phone-input-2
<PhoneInput
country={"in"}
value={values.phone}
name="phone"
onChange={(phone, data, e) => handleChange(e)}
onBlur={(e) => handleBlur(e)}
defaultMask=".... ... ..."
masks={{ in: ".... ... ..." }}
onlyCountries={["in"]}
inputProps={{
name: "phone",
required: true,
autoFocus: true,
}}
disableSearchIcon={true}
disableDropdown={true}
containerClass="mt-1 border h-12 text-sm focus:outline-none block w-full bg-gray-100 dark:bg-transparent border-transparent focus:bg-white"
inputClass="mt-1 border h-12 text-sm focus:outline-none block w-full bg-gray-100 dark:bg-transparent border-transparent focus:bg-white"
inputStyle={{
background: "transparent",
border: "1px solid grey",
height: "3em",
width: "100%",
outline: "none",
}}
buttonStyle={{
height: "3em",
background: "transparent",
outline: "none",
border: "none",
}}
/>
{handleFormikError(errors, touched, "phone")} // returns the span.alert with error message