New Member
I came across a phone number that ended in a zero-width, non-printing (U+202C, "Pop Directional Formatting").
This could be fixed either by cleaning up the database or in client side code by replacing all whitespace with a single whitespace, and then removing any characters except those in the ascii ranges of
I'll write in JavaScript for ease of translation to any other frontend or backend language:
function sanitize(tel) {
phone = phone
.replace(/\s+/g, ' ') // replace all whitespace with a single space
.replace(/[^ -;]/, '') // remove all characters except in the ascii range between space and semicolon (includes # + , . ( ) etc)
.trim() // trim leading and trailing whitespace
}
// Test cases
// I've pasted this with the U+202C (granted, it may not survive the BB submit)
sanitize(`+1 (385) 555-1337`) // `+ 1 (385) 555-1337`
// this could easily be extended to include alphas (e.g. p, w) if desired
sanitize(`+1 (ban) ana-1337`) // `+1 () -1337`
sanitize(`+ 1 (385) 555-1337#10,2,2;2*`) // `+ 1 (385) 555-1337#10,2,2;2*`
sanitize(" ") // ""
Also, since there are a very limited number of phone number formats around the globe, this could be further cleaned up in terms on consistent formatting by recognizing the +1 or country of the individual, removing non-touch-tone separator characters ( ) . - and leave control characters like # , etc and if the length of the number before # matches the expected number of characters and pattern, then rewrite the number in canonical form in the database (e.g. +13855551337), and apply a client-side formatter to represent a localized form (e.g. (385) 555-1337).
Similarly, a pattern could be used to apply formatting to invalid data for easier recognition by ward leaders who are trying to clean up the data.
This could be fixed either by cleaning up the database or in client side code by replacing all whitespace with a single whitespace, and then removing any characters except those in the ascii ranges of
I'll write in JavaScript for ease of translation to any other frontend or backend language:
function sanitize(tel) {
phone = phone
.replace(/\s+/g, ' ') // replace all whitespace with a single space
.replace(/[^ -;]/, '') // remove all characters except in the ascii range between space and semicolon (includes # + , . ( ) etc)
.trim() // trim leading and trailing whitespace
}
// Test cases
// I've pasted this with the U+202C (granted, it may not survive the BB submit)
sanitize(`+1 (385) 555-1337`) // `+ 1 (385) 555-1337`
// this could easily be extended to include alphas (e.g. p, w) if desired
sanitize(`+1 (ban) ana-1337`) // `+1 () -1337`
sanitize(`+ 1 (385) 555-1337#10,2,2;2*`) // `+ 1 (385) 555-1337#10,2,2;2*`
sanitize(" ") // ""
Also, since there are a very limited number of phone number formats around the globe, this could be further cleaned up in terms on consistent formatting by recognizing the +1 or country of the individual, removing non-touch-tone separator characters ( ) . - and leave control characters like # , etc and if the length of the number before # matches the expected number of characters and pattern, then rewrite the number in canonical form in the database (e.g. +13855551337), and apply a client-side formatter to represent a localized form (e.g. (385) 555-1337).
Similarly, a pattern could be used to apply formatting to invalid data for easier recognition by ward leaders who are trying to clean up the data.