View Full Version : strip out phone number characters

01-11-2004, 02:51 AM
I found this while searching for answers to my question.

Originally posted by Vincent Puglia

Before they move this to the db section...

1) step through the field
2) check each char
3) if not integer, discard
4) if integer, concatenate to var
5) when finished with field, update with var.

I'm assuming you really don't want to convert the phone number to an integer -- it's not (because you don't add, etc them) But, if you really want to:
use intval()


I want to do just that....evaluate each character and save only the numerical characters, but I don't know how to do this type of read (character in string).

I'm new to JS. Please throw me a bone. I know about string length and loops.


01-11-2004, 03:16 AM
string.replace( /\D/g, '' ); => String with all non-digits stripped out. (Note that it will not change the original string, but return a new processed string.)

Was this what you wanted?

01-11-2004, 03:26 AM
Thanks for the help....but that is like a foreign language to me. :confused:

What I woudl like to do....is set the value of a form field to a variable....loop thru the variable.....looking for common phone number characters like '(' and '-' or ')' . I would create a new string variable that contains only the numerical characters and put THAT string into the form textfield.

Basically, I want the user to be able to put in their phone number in whatever format they want (555) 555-1221 or 555-555-1221 and have the actual form value be the string "5555551221". My data goes into a backend file and I can format later.

I know how to do this in C++, but I'm VERY (like less than a week) new to JS and have the syntax knowledge of what I've learned over just one project.


01-11-2004, 03:37 AM
Then it seems you want to use the code I gave you. replace is a method of String instances such as the value of a form field. It takes a regular expression and a string as arguments, and will replace matches of the regular expression with the string in question. The regular expression I gave you (/\D/g) will match all non-digits, and they will be replaced by the string ''. The result will be sent as the return value from the function, a return value which you can for example write back to the originating form field, or even better write to a hidden form field, leaving the original form field as the user wrote it.

Willy Duitt
01-11-2004, 03:47 AM
Here is an example:

<script type="text/javascript">
function chkNum() {
theNum = document.forms[0].Num.value;
theNum = theNum.replace( /\D/g, '' );

Enter Phone Number: <input type="text" name="Num">
<input type="button" value="Check It" onclick="chkNum()">


01-11-2004, 04:11 AM
:) Thanks Willy and liorean.

I've taken both your replies and now have just what I was looking for.

I learn by example, so I appreciate the guiding syntax. I also took liorean's advice and put the value returned by the function to a hidden field. I could anticipate user's going back over and over to type in their phone number if they saw I had changed it.

Thanks to both of you!:thumbsup:

01-11-2004, 04:42 AM
How do I evaluate the length of a textfield on my form? I thought I knew this :rolleyes:

if(document.REG.wc_STRIPPEDPHONE.length != 10){
alert("Please provide a valid phone number including area code.");

01-11-2004, 04:45 AM
Had! to be something simple didn't it:(

if(document.REG.wc_STRIPPEDPHONE.value.length != 10){
alert("Please provide a valid phone number including area code.");
return false;