Jump to content

Removing every non-numeric character from a string and character arrays.


pthurmond

Recommended Posts

I am trying to take phone number input from users and remove everything but the numbers from the string, so that I can then format it how it wish. Is there a function to remove all non-numeric characters? Also can the explode function be used to separate a string into an array of its characters (for testing such as passwords or phone numbers)? If not, does a function exist that can?

Thanks,
Patrick
[quote author=pthurmond link=topic=121575.msg500141#msg500141 date=1168298233]
I am mainly concerned about special characters such as dashes and parenthesis.
[/quote]
Then you need to be very specific. What characters fall into the "such as" category?

Typically I have done phone number formatting on the client side using javascript. That is not 100% since the user's can have JS turned off, but it is a good first line of defense. But, the methodology would be the same.

The process would go something like this:
1. Strip all non-numerica characters from the string
2. Check the length (usually must be 7 or 10 characters, but could be a range as well - depends on the situation). If the length is not correct, then alert the user to change it.
3. If the length validation passes then reformat the string how it should be(e.g. (123) 456-7890 or 123-456-7890, etc.)

However, you *may* want to allow alpha characters so the phone could be something like 1-800-GETMORE. So, are you wanting to exclude certain characters or are you wanting to only limit it to certain characters. If you can provide specific criteria, it will be easier to provide the solution you are looking for.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.