Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format

Status
Not open for further replies.

JPBURBE

Programmer
Jun 12, 2001
37
CA
How can i format a field so it has the following:


########-#

This is what i tried but it doesn't work!Help please
num = 999999999
numStr = new String(num);
newStr =
numStr.substring(0,numStr.length-1)+'-'+numStr.substring(numStr.length-1)
 
to understand why, see :
now, your code is :
<SCRIPT LANGUAGE=&quot;JavaScript1.2&quot;>
re = /(^\d)(\d+)/;
str = &quot;99999999&quot;;
newstr = str.replace(re, &quot;$1-$2&quot;);
document.write(newstr)
</SCRIPT>
------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
Thanks, i'll try it.....just a quick question:

What does this [re = /(^\d)(\d+)/;] do?
 
JPBURBE

Your logic should work. It's just that your syntax is wrong.
In JavaScript, you can do a numeric to string casting by adding the number to a text string.
So
numStr = new String(num);

to be replaced by

numStr=''+num;


Then
newStr =
numStr.substring(0,numStr.length-1)+'-'+numStr.substring(numStr.length-1)

to be replaced by

newStr =
numStr.substring(0,numStr.length-1)+'-'+numStr.substring(numStr.length-1,numStr.length)

Regards
 
hey......it didn't work.......i am trying to format a field in a PDF document so it looks like that.....when i tried to put the script language tag in it told me i had a syntax error so i took it out and it still wouldn't work
 
JPBURBE,
Which one doesn't work?
The one that I corrected your syntax or the one posted by iza?
I tested the former and it works.
 
That didn't work either.....If i type in 123456789 it leaves it like that instead of putting it 12345678-9!
 
the one you corrected......i don't know if it's because it is a pdf file or what but i tried it and it didn't work!
 
Don't forget to put ; at the end of newStr =
numStr.substring(0,numStr.length-1)+'-'+numStr.substring(numStr.length-1,numStr.length);




 
it's there and still nothing! Thanks by the way for the help.......
 
the code i gave you works for sure (tested !!)

&quot;What does this [re = /(^\d)(\d+)/;] do?&quot;
--> ah ah that's why i gave you the url BEFORE the code - anyway .... this is a reg exp and it states that the expression it's looking for is of the form : xy where x is A SINGLE number and y is 1 or more numbers
and then the &quot;str.replace(re, &quot;$1-$2&quot;)&quot; line says to write the expression this way : x-y
so if str was &quot;999999999&quot; you'll get x=9 and y=99999999 and it'll display 9-99999999 ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
i am not just entering the information onto a form.....the information is coming from a vb program. the dash is in the vb program but on the PDF form it doesn't show up!
 
there was noting about a form in any thread here - or did i miss that ???
and where the info comes from is not a problem, as you're only using a string - and in javascript pretty much anything can be a string - so all code given here should be working regardless you're writing a form, reading datas from vb or from a text file or anything else
now i've got troubles understanding this : &quot;the dash is in the vb program but on the PDF form it doesn't show up&quot;
- the string you're reformatting is &quot;xxxx&quot; or &quot;x-xxx&quot; ???
- you've got a vb program, you'll send datas to a web page that is sending them to a pdf ???
- where do you want the string to be formatted : in the web page or in the pdf ?


------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
i am not reformatting. I want to custom format a textbox. The vb sends the info straight through to the PDF file....i want to format the textbox in the PDF file
 
pdf ??? i could have helped you for html but i don't know how pdf & javascript interact with each other
and about &quot;my&quot; fuction (the one i gave you above), is it running as you wish ? ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
no......neither of the suggestions given to me are working.....If you can think of anything please get back to me...
 
what is not working ? it's not displaying anything or is it ? ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
The &quot;-&quot; is not showing up in the number
 
you call my function with str=&quot;999999&quot; and what writes is &quot;999999&quot; (same lenght) ? then it's a pdf problem, not related to javascript functions but rather to pdf display

and i'm totally unable to help you with pdf ! ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top