Forum Moderators: open
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.
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.
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.
Call the function like this:
<p><%=formatPhone(qryDetails.Fields.Item("Telephone").Value)%></p>
That should do the trick.
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.
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.
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
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'Format'
I don't think Format() function is supported ASP/VBScript but rather in VB.
<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.
Also, I think "Type mismatch: 'Format'" may have to do with your database. What format is your Telephone field in?
Onya
Woz
Telephone field is of:-
Data type: Text
Size: 12 [999-999-9999]
Format: @@@-@@@-@@@@
Input Mask: 999-999-9999
Database: Access 2000.
Any ideas ?
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
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.
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.