How do I remove hyphens,brackets, dots, etc from a telephone no. in Javascript

When a user enters some text in a textbox, say telephone no. in any form
such as 123.456.7890 or (123)4567890 or (123)-456-7890 or (123).456.7890
or in any format which contains only numbers and it has to contain exactly 10 numbers,
it ha sto be converted to the format 1234567890. How can this be done in javascript?
and also if the user enters a zipcode in any format, such as 12345 or 12345-6789, it has to be converted to 123456789 , it can be 5 digits or 9 digits long and also if the user enters tax id in any format, it has to be converted to the format 123456789....exactly 9 digits long.

It's extremely urgent. if you could send me the code, It'll be greatly appreciated.
Thanks a million in advance!
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

naspinskiConnect With a Mentor Commented:
here is a function that will remove all your unwanted characters (add more if you want) with regualr expressions and return the proper formatted string if it is the proper length specified otherwise it will return false:
function makeNumber(data, length) {
    var out = data.replace(new RegExp("[-|(|)|\.]", 'g'), '');
    return (out.length == length ? out : false);

Open in new window

Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
To stop users from entering non-aphanumeric characters or white spaces.

Using regular expressions would be the easiest method:

Here is my javascript code:

<script language="Javascript">
function alphaNumericCheck(){
var regex=/^[0-9A-Za-z]+$/; //^[a-zA-z]+$/
return true;
} else {
alert("Please fix: password")
return false;

For numbers only use /^[0-9]+$/

For mixed text and numbers, with spaces /^[0-9a-zA-Zs]+$/

Here are more useful regular expressions:

[a-zA-Z] any letter
d any number; same as [0-9]
D any NOT number; same as [^0-9]
w any alphanumeric character; same as [a-zA-Z-0-9_]
W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
s any whitespace (tab, space, newline, etc...)
S any NON-whitespace
n newline
t tab

(orginally from :
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
ok here is what exactly you want:

<input name="realname" type="text" size="<%= box_size %>" maxlength="100" alt="Your name" onKeyPress = "checkNum()">

to set fixed length for text box:
in script set: box_size = 10 (as per ur requirement)

to validate only numbers entered:

//Makes sure that the entered value is a number
// use via onKeyPress="checkNum()" on the textbox
function checkNum(){
var carCode = event.keyCode;if ((carCode < 48) || (carCode > 57)){
alert('Please enter only numbers.');      event.cancelBubble = true      event.returnValue = false;      }}
amuktaAuthor Commented:
Hi! Sree Ven,
The user can enter the numbers with the '.', "(","-") in between the numbers like 123-456.7890 or the like, but it should convert it into the format "123456789" when it has to be saved in the database.

If the user enters for eg:123A-456-7890,the user should be prompted to correct it.

Then the first one would be appropriate or any additonal code is needed?

Please do let me know ASAP.Thanks a lot in advance!

amuktaAuthor Commented:
Thank you!
All Courses

From novice to tech pro — start learning today.