...

View Full Version : Format phone number in dynamic text?



ConnieM
12-10-2004, 05:57 PM
On an ASP Details page, I have dynamic text with a company's address, phone and fax numbers. I want the phone and fax numbers to be formatted like (###) ###-#### on the screen. They are entered in MS Access in 10-digits only.

I am using DWMX 2004 and am not a programmer or coder by any means. I create my recordsets in DWMX in simple mode, so if you can help me, please be specific. My field names are MbrMainPhone and MbrFax if that helps.

Thanks!

Basscyst
12-10-2004, 06:34 PM
Something like this?



<script type="text/javascript">
function formatPhone(str)
{
var npa=str.substring(0,3);
var nxx=str.substring(3,6);
var line=str.substring(6,10);
var formatted="("+npa+") "+nxx+"-"+line;
return formatted;
}
var num=formatPhone('9165551212');
alert(num);
</script>


Just pass the number to the function as I did above.

Basscyst

ConnieM
12-10-2004, 07:34 PM
Basscyst, thanks for the response. I tried entering that into my code and it "sort-of" works but not quite what I was looking for. What happens is when I click on a company name to open up the details page (that has address, phone numbers, e-mail address, etc.), it comes up with a small pop-up window that formats the phone number, then you have to click OK, then it brings up my details page with the phone number as normal (5554443333).

Any suggestions?

Willy Duitt
12-10-2004, 08:25 PM
Basscyst example is merely that... An example...
The alert was added for the sole purpose of demonstrating the code....

You need to take the code and work it into your application (removing the offending demonstrative alert) and since you have not provided any code there is not much more that anyone can do other than providing an example as Basscyst has done....

.....Willy

ConnieM
12-10-2004, 09:25 PM
Willy, maybe this will help. I created a blankpage with the "bare bones" of what I will have on the page (company name & phone number):


<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include virtual="/Connections/datasource.asp" -->
<%
var rsCoDetail = Server.CreateObject("ADODB.Recordset");
rsCoDetail.ActiveConnection = MM_datasource_STRING;
rsCoDetail.Source = "SELECT MbrCompany, MbrMainPhone FROM w_allcompanies";
rsCoDetail.CursorType = 0;
rsCoDetail.CursorLocation = 2;
rsCoDetail.LockType = 1;
rsCoDetail.Open();
var rsCoDetail_numRows = 0;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<%=(rsCoDetail.Fields.Item("MbrCompany").Value)%>
<br>
<%=(rsCoDetail.Fields.Item("MbrMainPhone").Value)%> (this is the line that I want to show up as formatted like (555) 444-3333)
</body>
</html>
<%
rsCoDetail.Close();
%>

Basscyst
12-10-2004, 11:28 PM
Don't work with asp too much but I think this might work, since you are using JS anyhow.



<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include virtual="/Connections/datasource.asp" -->
<%
var rsCoDetail = Server.CreateObject("ADODB.Recordset");
rsCoDetail.ActiveConnection = MM_datasource_STRING;
rsCoDetail.Source = "SELECT MbrCompany, MbrMainPhone FROM w_allcompanies";
rsCoDetail.CursorType = 0;
rsCoDetail.CursorLocation = 2;
rsCoDetail.LockType = 1;
rsCoDetail.Open();
var rsCoDetail_numRows = 0;

function formatPhone(str)
{
var npa=str.substring(0,3);
var nxx=str.substring(3,6);
var line=str.substring(6,10);
var formatted="("+npa+") "+nxx+"-"+line;
return formatted;
}
var num=formatPhone(rsCoDetail.Fields.Item("MbrMainPhone").Value);

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<%=(rsCoDetail.Fields.Item("MbrCompany").Value)%>
<br>
<%=(num)%>
</body>
</html>
<%
rsCoDetail.Close();
%>

glenngv
12-11-2004, 08:57 AM
function formatPhoneNumber(num)
if (num == "" || num == null) {
return "";
}
else {
num = "" + num;
var numLen = num.length;
if (numLen == 10)
return "(" + num.substr(0, 3) + ") " + num.substr(3, 3) + "-" + num.substr(6, 4);
else if (numLen == 7)
return num.substr(0, 3) + "-" + num.substr(3, 4);
else
return num;
}
}

Willy Duitt
12-11-2004, 02:10 PM
I like using regular expressions....
However, this example answers Connie's question as asked to format a 10 digit number and if there is a chance that 7 digit numbers will also be used some changes will need to be made...
(call the function onload and pass the form name and input name as an arguement thru the function)



<script type="text/javascript">
<!--//
function formatPhoneNumber(input){
if((/^(\d{3})(\d{3})(\d{4})$/).test(input.value)){
input.value = '('+RegExp.$1+') '+RegExp.$2+'-'+RegExp.$3;
} else{ input.value = input.value }
}
//-->
</script>
</head>

<body onload="formatPhoneNumber(document.forms[0].MbrMainPhone)">
<form>
<input name="MbrMainPhone" value="5554443333">
</form>


....Willy

Basscyst
12-11-2004, 06:23 PM
Hey,

Two guys that posted after me, that have done more learnin' than me, did I do that asp edit right? Just curious. :cool:

Basscyst

Willy Duitt
12-11-2004, 08:08 PM
I couldn't tell you....
I don't know a dang thing about ASP... :eek:

Well other than an asp is also known as a snake...
And snakes are also known as vipers...
And Vipers are one heck of a car...

.....Willy

Brandoe85
12-11-2004, 08:20 PM
I wonder which one they will use this one, or this (http://www.codingforums.com/showthread.php?t=48776) one :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum