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
Solved

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

Posted on 2009-04-07
5
618 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
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…

839 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