-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confused by formatting inputs example on home page #108
Comments
Hmm. Anyone here that can help? |
@pkreipke Here's what I did to format as a US # (works in example app): (lol github code tag fails on the block) const formatPhoneNumber: string = phoneNumberString => {
//normalize string and remove all unnecessary characters
phoneNumberString = ('' + phoneNumberString).replace(/[^\d]/g, "");
//check if number length is at least 10, if longer strip off the end characters
if (phoneNumberString.length >= 10) {
//reformat and return US phone number
return phoneNumberString.substring(0,10).replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
}
//let them keep typing in their phone number
return phoneNumberString
} then in form:
function cobbled together from a few answers @ https://stackoverflow.com/questions/8358084/regular-expression-to-reformat-a-us-phone-number-in-javascript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this example Formatting Inputs I'm confused by this line:
const formatPhoneNumber: string => string = (unformattedPhoneNumber) => ...;
Why the double function indirection? How exactly is that adding spaces? I'm guessing it's just a sample to say that if "value" prop is a function, it'll get called but store the value untransformed.
Would love some hints.
The text was updated successfully, but these errors were encountered: