Solved

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

Posted on 2009-04-07
5
614 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This article discusses how to create an extensible mechanism for linked drop downs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now