Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-07
5
Medium Priority
?
624 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 1500 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

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

722 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