Solved

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

Posted on 2009-04-07
5
621 Views
Last Modified: 2012-05-06
Hi!
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!
0
Comment
Question by:amukta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24094169
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]+$/
if(regex.test(document.add_data.password.value)){
alert("Good")
return true;
} else {
alert("Please fix: password")
return false;
}
}
</script>

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 : http://newsourcemedia.com/home.php?view=105)
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24094201
ok here is what exactly you want:

textbox
<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;      }}
0
 
LVL 21

Accepted Solution

by:
naspinski earned 500 total points
ID: 24094260
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

0
 

Author Comment

by:amukta
ID: 24098632
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!


0
 

Author Comment

by:amukta
ID: 24716732
Thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question