Forum Moderators: open

Message Too Old, No Replies

How do I format 9999999999 into 999-999-9999 ?

         

spremji

9:48 pm on Dec 7, 2001 (gmt 0)



Hi all,

Newbie. I am using ASP with Access 2000 DB.

I have a Telephone & Fax column with numbers, say, 123456789 as Text data type.

So, I formatted the numbers within Access to display as 123-456-7890 and it shows perfectly fine within Access. However, when I view the html page displaying the Tel field, it's like 1234567890.

How do I format it within .asp file ?

Clear & concise explanation and code illustrations would highly be appreciated and understood.

Thanks.

txbakers

10:22 pm on Dec 7, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need a simple Javascript routine on the returned data field:

function formatPhone (phnum) {
var newph1 = phnum.substr(0,3);
var newph2 = phnum.substr(3,3);
var newph3 = phnum.substr(6,4);
var newph = newph1 + "-" + newph2 + "-" + newph3;
}

Then, in your browser, wherever you want to show the formatted phone number, instead of just using the ASP code for the field, put the javascript function around it. (with the script tags, of course).

I'm sure there are other more elegant ways of doing this, but this should be clear.

spremji

8:59 pm on Dec 8, 2001 (gmt 0)



Looks very elegant to me....

So if I have the following code:

<html>
<head>
<title>Contacts</title>
</head>

<body>
<p><%=(qryDetails.Fields.Item("Telephone").Value)%></p>
</body>
</html>

Where do I put the function and how do I call it inside my code?

Thanks.

Will

10:04 pm on Dec 8, 2001 (gmt 0)



The function can go anywhere on your page, provided you surround it with the <% and %> tags. Generally speaking, it's best to keep all your functions together, either at the top/bottom of the page, or in a separate include file so you can use them on multiple pages.

Call the function like this:

<p><%=formatPhone(qryDetails.Fields.Item("Telephone").Value)%></p>

That should do the trick.

txbakers

11:40 pm on Dec 8, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not entirely correct! If you use the function like Will wrote:
<p><%=formatPhone(qryDetails.Fields.Item("Telephone").Value)%></p>

you will get an error message saying "formatPhone is undefined."

You need to use it like this:
<script>formatPhone("<%=qryDetails.Fields.Item("Telephone").Value)%>")</script>

Although both sets of code are Javascript, the ASP code inside the delimiters is server side and the formatPhone function is client side. You can use the <%= %> tags inside regular javascript functions.

spremji

2:25 am on Dec 9, 2001 (gmt 0)



Will: code didn't work.

txBakers: I got an error something to do with ")" using:

<script>formatPhone("<%=qryDetails.Fields.Item("Telephone").Value)%>")</script>

So, I tried deleting the first ")" after .Value like

<script>formatPhone("<%=qryDetails.Fields.Item("Telephone").Value%>")</script>

Result: gave me error.

I tried putting "(" in front of qryDetails and ")" after .Value like:

<script>formatPhone("<%=(qryDetails.Fields.Item("Telephone").Value)%>")</script>

Result: It doesn't give error but the number is not displayed at all !!

I know it's just a simple thing but please help.

Thanks again.

Woz

2:46 am on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



spremji,

you can do the whole thing in ASP.

Change

<%=(qryDetails.Fields.Item("Telephone").Value)%>

to

<%= Format((qryDetails.Fields.Item("Telephone").Value), "(###)###-####")%>

which should give you your number in the format (999)999-9999

if you only wany 999-999-9999, then change the mask to ###-###-####

I've not tested this specifically but it should work.

Onya
Woz

txbakers

5:00 am on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't realize there was a format function in Javascript! Does this work client side as well?

spremji

6:35 am on Dec 9, 2001 (gmt 0)



Woz: I tried the Format() but it gave me the following error:

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'Format'

I don't think Format() function is supported ASP/VBScript but rather in VB.

txbakers

6:49 am on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried it straight away as well, and got the same error.

I don't recall format being a javascript function. In VB it works.

Did my first routine ever work for you?

spremji

6:54 am on Dec 9, 2001 (gmt 0)



txBakers: I got an error something to do with ")" using:

<script>formatPhone("<%=qryDetails.Fields.Item("Telephone").Value)%>")</script>

So, I tried deleting the first ")" after .Value like

<script>formatPhone("<%=qryDetails.Fields.Item("Telephone").Value%>")</script>

Result: gave me error.

I tried putting "(" in front of qryDetails and ")" after .Value like:

<script>formatPhone("<%=(qryDetails.Fields.Item("Telephone").Value)%>")</script>

Result: It doesn't give error but the number is not displayed at all !!

I know it's just a simple thing but please help.

Thanks again.

Woz

7:12 am on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Format () is VB but can be called through ASP. As I say I have not tested this as yet, but you can refer here [4guysfromrolla.com].

Also, I think "Type mismatch: 'Format'" may have to do with your database. What format is your Telephone field in?

Onya
Woz

spremji

7:27 am on Dec 9, 2001 (gmt 0)



Woz:

Telephone field is of:-

Data type: Text
Size: 12 [999-999-9999]
Format: @@@-@@@-@@@@
Input Mask: 999-999-9999

Database: Access 2000.

Woz

7:41 am on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try resetting the field to Number format and see what happens.

txbakers

5:54 pm on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just remembered something regarding VB in ASP. By default, the language you choose is at the top, but you can override it and mix languages, as long as your declare it when you need to.

<script language="VBScript" runat = "server"> use your format function here </script>

this might work.

spremji

9:11 pm on Dec 9, 2001 (gmt 0)



Woz: I think you might be right with changing the Telephone field to Number data type. However, when I do this in Access, it deletes all Telephone data in the field. Since I have more than 1000 records, I cannot afford to lose my Tel and Fax field data.

Any ideas ?

Woz

9:44 pm on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



spremji,

First, make a backup of your entire database for safety!

Secondly, IF YOU HAVE MADE YOUR BACKUP!!!!!

try adding an additional field in number format with the correct masks etc and copy all your telephones into this field.

Then try the asp on this field.

If it works, rename the old telephone field and rename the new field with the name the old telephone field used to have. As long as the new field has the old telephone field name and no fields are deleted, this will insert the new field into any relationships the old field had. (Clear?)

Then if all relationships are OK and you still have all the numbers in the new field, delete the old field.

good luck.

Onya
Woz

txbakers

10:27 pm on Dec 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



or just use my little javascript routine to format it.

Will

1:43 pm on Dec 10, 2001 (gmt 0)



Man, this is getting over complicated.

The post I made earlier is correct - it assumes that you are doing all processing on the server using ASP.

You can use a Javascript routine on the client, but it won't run if they don't support Javascript or have it switched off, so the number will not be formatted correctly.

<script language=javascript runat=server>
format function goes here...
</script>

Then use
<%=(qryDetails.Fields.Item("Telephone").Value)%>

If you want to use the Javascript version on the client, you need to alter the earlier example slightly.

<script>formatPhone("<%=qryDetails.Fields.Item("Telephone").Value)%>")</script>

There is a bracket after the word "Value" that should not be there.

spremji

5:04 pm on Dec 10, 2001 (gmt 0)



I don't know where I am going wrong but it's really frustrating.

Woz: I renamed the old Telephone field to Telephone_old and inserted new field called Telephone with data-type Number. When I tried copy-pasting the Tel data from the old field to the new one, Access gave me an error saying that I cannot put a text value into a number data-type. So I manually typed the data in one record for testing purposes. Tested on my browser but still in vain.

Man, this is just crazy....I cannot believe how such a little thing consumes my entire day. I cannot roll-out the project for this little stupid thing.