Closed timrombergjakobsson closed 5 years ago
What's the exact issue you're having?
Without any of the yup
stuff.
That regardless in what format I write the phone number into the input that uses the validation I get the message: 'Not a valid number'.
Provide an exact code sample
@catamphetamine ok, I posted above how Im defining the validation schema, and then I have a validation method that uses the ValidationSchema like this:
validate = values => {
let validationErrors = {};
try {
validateYupSchema (
values,
ValidationSchema(), <--- here I use the imported function
true, // synchronous validation
);
} catch (err) {
validationErrors = yupToFormErrors(err);
return validationErrors;
}
const activePage = React.Children.toArray(this.props.children)[
this.state.page
];
return activePage.props.validate ? activePage.props.validate(values) : {};
};
Don't post any "yup" references.
Post a minimal and simple code sample which only uses libhphonenumber-js
.
Ok then:
import { parsePhoneNumber } from 'libphonenumber-js'
try {
const phoneUtils = parsePhoneNumber(value, 'SE')
const phoneNumber = phoneUtils
return phoneUtils.isValidNumber(phoneNumber)
} catch (e) {
return false
}
Simplify further, until it's the simplest code.
import { parsePhoneNumber } from 'libphonenumber-js'
try {
return parsePhoneNumber(value, 'SE')
} catch (e) {
return false
}
What's your issue with the code posted above?
Hmm this wont work return phoneUtils.isValidNumber(parsePhoneNumber(value, 'SE'))
as I need to define phoneUtils
. But besides that it's as I mentioned. When using this to validate a swedish mobile phone number it fails. I tried to enter a number in both the formats +477xxxxx
and 07xxxx
but it still does not pass validation.
If you don't post an exact minimal code sample illustrating the issue in your next comment I will close this issue.
Ok, it might be late but I dont understand what you mean. I have posted code examples in several comments. you mean a codesandbox or plunker or what do you mean?
Ok, it might be late but I dont understand what you mean. I have posted code examples in several comments. you mean a codesandbox or plunker or what do you mean?
No, you should post a simple minimal example illustrating the issue you're having with no non-defined variables and not spread across several comments.
Ok, I tried your example, return parsePhoneNumber(value, 'SE')
and now it works. Thank you.
For validation use:
parsePhoneNumber(value, 'SE').isValid()
Hi, I Im using the lib in a React app that Im building, and Im trying to use it together with yup. Im using it like this:
I verified it here https://libphonenumber.appspot.com/phonenumberparser?number=+467567891&country=SE And here https://libphonenumber.appspot.com/phonenumberparser?number=0734448472&country=SE and it works. Although when entering the phone number in my app I get the message saying that it is not a valid number, regardles the preceeding
+46
or just07
? What am I missing?