View Full Version : Stripping out telephone numbers

Philip M
06-20-2002, 07:52 PM
I have a form with a textbox and I already have a "smut engine" script which turns bad words into ******.

What I would like to do is prevent people entering telephone numbers, and similarly turn them into *******. What I have in mind is that a phone number is recognised as being 8 consecutive digits but including blanks or hyphens.

E.G. 1234 5678 or 1234-5678 or 012-345-6789 recognised as phone numbers.

I don't seem to be having much success in developing a working script. Can anyone point me the right way?

06-20-2002, 08:02 PM
What exactly do you want to do?

Mask phone numbers with *, or validate them?

I've written a few regular expressions that do the job nicely on request... but I need to know exacty what you want.

Philip M
06-20-2002, 08:04 PM
Thanks jkd. I want to mask the phone numbers, that is replace them with asterisks. I do not want to validate them, but I am assuming that any string of eight or more consecutive digits (plus blanks and hyphens if any) entered into the text box is a phone number, and thus to be masked.

06-20-2002, 08:25 PM
Well, this masks all input:

<input type="text" onkeyup="this.value = this.value.replace(/[^\*]/g,'*')"/>

But I'd assume you'd like somewhere to store their real phone number?

This *sort* of works:

<input type="text" onkeyup="if (typeof this.realValue == 'undefined') this.realValue = ''; this.realValue += this.value.charAt(this.realValue.length); this.value = this.value.replace(/[^\*]/g, '*')"/>

But if you type too fast, realValue will contain some * oddly enough - it seems some of the code inside the event is happening asynchronously.

Philip M
06-20-2002, 10:01 PM
Thanks once again jkd.

But I don't want to mask all numeric input - just strings of numbers which are or might be telephone numbers. That is, 8 or more consecutive digits.

To spell it out, I do not want people to be able to post things like

"Earn lots of money with my MLM scheme - telephone 1234-5678".

but (for example) things like "Price $1234.56" or "serial number 987654" are OK.

I do not want to store their real phone number.

Hope I have clarified the objective!

By the way, only spaces and hyphens in the numeric string should be counted. This is because something like "1984/85/86/87" is OK. I realise that my idea will block the dates "1985-1986" for example, but this is not a serious problem in the context.

So to recap: 1995/96/97/98 is OK
$1,234,567,891 is OK
1234-5678 or 123-4567-8912 are not and to be masked out with *.

06-21-2002, 03:15 AM
Actually this regexp that jkd and I worked on (;)) might help:

<script type="text/javascript">
var vphone = /^\D*([1-9]\d{2})\D*(\d{3})\D*(\d{4})\D*$/
function formatphone(x){
x.value=x.value.replace(vphone,'($1) $2-$3');
else {
alert('Please enter a valid phone number!');
// -->

That will match anything that contains a format of

(anything) NNN (anything) NNN (anything) NNNN (anything)

Other than something like that, I think it would take human intervention!

06-21-2002, 03:18 AM
P.S. If it matches, you can use the above code to replace any numbers with * - but anyone who is savvy will figure that out, and figure out a way around it. You can only do so much... I think that in this case you might want to hire a kid to do this for cheap.

Philip M
06-21-2002, 07:51 PM
Whammy and Dave Clark - thank you both once again for most helpful advice. You may be right - human intervention may be the solution!!