Normal Topic Formating a Telephone Number - basic and advanced (Read 5988 times)
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Formating a Telephone Number - basic and advanced
Feb 23rd, 2004 at 2:25pm
Print Post Print Post  
// Formating a Telephone Number replace Workphone with your element.
// this method only formats the number

workphone = "(" + @Left(WorkPhone, 3) + ") " + @Mid(WorkPhone, 4, 3) + "-" + @Right(workPhone, 4) 

// This is a step by step method that helps when formating and using with other code

var vwPhone as String
var vNewwPhone As String

vwPhone = @Num(WorkPhone)
vNewwPhone = "(" + @Left(vwPhone, 3) + ") " + @Mid(vwPhone, 4, 3) + "-" + @Right(vwPhone, 4)
WorkPhone = vNewwPhone 




// Formats Telephone Number - this version checks for proper amount of digits and
// notifies user if there is a problem my element is named HomePhone

var vHPhone as String    
var vNewHPhone As String 
var vhphonecount
var vhpnentered

     Vhpnentered = @num(HomePhone)
     
     
     vhphonecount = @num(@len(vhpnentered))
     

If vhphonecount = "10"


{
     @color(HomePhone,0,7)
     vHPhone = @Num(HomePhone)
     vNewHPhone = "(" + @Left(vHPhone, 3) + ") " + @Mid(vHPhone, 4, 3) + "-" + @Right(vHPhone, 4)
     HomePhone = vNewHPhone 
}
else
{
     @color(HomePhone,7,1)
     @msgbox("The Telephone Number needs to be Exactly 10 Digits", "The numbers you entered " +
     @num(Homephone),"is : " + Vhphonecount + " Digits Please check Home Telephone and Re-enter")
     
}
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Formating a Telephone Number - basic and advan
Reply #1 - Feb 23rd, 2004 at 3:52pm
Print Post Print Post  
Here is a telephone number formatter that uses a "template". It does not allow the user to type characters that do not match the template. In the template the character: "x" represents any numeric character. All of the other characters in the template must be matched exactly.

This must be placed in the immediate change event of the LE. It assumes that the LE is called "Phone".

var contents as string
var template as string
var ch as string
var ch1 as string
var ch2 as string
var length as int
var loop as int
var cutoff as int

template = "(xxx) xxx-xxxx"
contents = Phone

length = @len(contents)
cutoff = 0

for loop = 1 to length
       if(@mid(template, loop, 1) = "x") then
       {
               ch = @mid(contents, loop, 1)
               if((@asc(ch) < @asc("0")) or (@asc(ch) > @asc("9"))) then
               {
                       if(cutoff = 0) then
                       {
                               cutoff = loop
                       }
               }
       }
       else
       {
               ch1 = @mid(template, loop, 1)
               ch2 = @mid(contents, loop, 1)
               if(ch1 <> ch2) then
               {
                       if(cutoff = 0) then
                       {
                               cutoff = loop
                       }
               }
       }
next

if(cutoff > 1) then
{
       Phone = @left(contents, cutoff - 1)
}
else if(cutoff = 1) then
{
       Phone = ""
}
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
KGMACLEOD
Member
*
Offline


No personal text

Posts: 9
Joined: Nov 12th, 2005
Re: Formating a Telephone Number - basic and advan
Reply #2 - Feb 16th, 2006 at 6:30am
Print Post Print Post  
I tried adapting this code for Social Security numbers and it works fine unless the number begins with a zero as many ss#'s do. Any suggestion on what I'm doing wrong. Or maybe what I'm not doing.  ???
  
Back to top
 
IP Logged
 
KGMACLEOD
Member
*
Offline


No personal text

Posts: 9
Joined: Nov 12th, 2005
Re: Formating a Telephone Number - basic and advan
Reply #3 - Feb 16th, 2006 at 6:33am
Print Post Print Post  
I should have mentioned I used the second format that "Bobscott" offered.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Formating a Telephone Number - basic and advan
Reply #4 - Feb 16th, 2006 at 2:05pm
Print Post Print Post  
Kristin,

This problem is usually because a text value (like something with a leading 0) is being converted to a number. 

Try something like this:
Code
Select All
FUNCTION FormatSS(vSS as String) as String
var vRet as String
var vNewSocial As String
var vSocialCount as Int
var vSocialEntered as String

	vRet = vSS
	vSocialEntered = @Num(vSS)
 	vSocialCount = @Len(vSocialEntered)

 	If vSocialCount > 0
	{
		If vSocialCount = 9
 		{
			vNewSocial = @Left(vSocialEntered, 3) +  "-" + @Mid(vSocialEntered, 4, 2) + "-" + @Right(vSocialEntered, 4)
			vRet = vNewSocial
		}
		Else
		{
			@msgbox("The Social Security Number needs to be Exactly 9 Digits", "The numbers you entered " +  vSocialEntered, "is " + @Str(vSocialCount) + " Digits.  Please Re-enter")
		}
	}
	Return(vRet)

END FUNCTION


MyValue= FormatSS(MyValue)
 

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged