473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Missing LEADING ZERO with script....please help.

Hi,

I have 2 text boxes on an ASP form.
A user enters a Serial Number in TB1 such as 0105123456, presses tab to
move to TB2, TB2 then displays the value of TB1 after a calculation has
been done.

(Based on a Serial Number range).
i.e. a user enters 0105123456, TB2 then adds 'x' qty to this number
depending on how many serial numbers are required.

Code as folows:

_________________________________

<script type="text/javascript">

var squantity = <%= request.querystring("Quantity") %>;

function doCalc(x,y){
y.value = x.value==0?0:1*x.value + (squantity - 1);
}

</script>

_________________________________

On the code for TB1, I have the following:
TB2 has the name 'txtLastSerial'

onblur="doCalc(this,this.form.txtLastSerial)"

_________________________________

THE PROBLEM:

If a user enters their 10 digit serial number, the first 4 digita are
month and year, (mmyy), i.e. 0105 123 456, then TB2 displays the
correct result but WITHOUT THE LEADING ZERO ?

How can I correct my code to leave the leading zero ?
Appreciate your help
David.

Jul 23 '05 #1
7 2846
wrote on 04 jan 2005 in comp.lang.javascript:
If a user enters their 10 digit serial number, the first 4 digita are
month and year, (mmyy), i.e. 0105 123 456, then TB2 displays the
correct result but WITHOUT THE LEADING ZERO ?

How can I correct my code to leave the leading zero ?
Appreciate your help


numbers do not have leading zeros, strings can have them.

<script type='text/javascript'>

t = '0105 123 456' // string

t = t.replace(/ /g,'') // strip spaces

t = t*1 + 1 // number, add 1

t = '' + t // string

while (t.length<10) t = '0' + t // add zeros

alert(t) // show string 0105123457

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2
Evertjan,

Many thanks for your reply.
I am not very proficient at JavaScript.
I sort of understand your code, but someone else wrote mine.

If the month is between January & September, the Serial Number will
always begin with a zero,
0105, 0205, 0305 etc...

All serial numbers this month will read 0105xxxxxx

The user will always type in text box 1 the correct number, it is only
when text box2 is reached that text box 2 displays the correct result
less the leading zero.

Are you able to take my code supplied and correct it for me ?

Thanks again
David

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
David Gordon wrote on 04 jan 2005 in comp.lang.javascript:
Many thanks for your reply.
I am not very proficient at JavaScript.
I sort of understand your code, but someone else wrote mine.

If the month is between January & September, the Serial Number will
always begin with a zero,
0105, 0205, 0305 etc...

All serial numbers this month will read 0105xxxxxx

The user will always type in text box 1 the correct number, it is only
when text box2 is reached that text box 2 displays the correct result
less the leading zero.

function doCalc(x,y){
y.value = (x.value==0) ? 0 : 1*x.value + (squantity - 1);
while (y.value.length<10) y.value = "0" + y.value;
}


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #4
Evertjan. wrote on 04 jan 2005 in comp.lang.javascript:
function doCalc(x,y){
y.value = (x.value==0) ? 0 : 1*x.value + (squantity - 1);
while (y.value.length<10) y.value = "0" + y.value;
}


1 x.value is a string that could be empty or text filled
2 you would not want 0000000000

function doCalc(x,y){
if(isNaN(x.value)||(x.value==0))
y.value = 0;
return;
}
y.value = 1*x.value + (squantity - 1);
while (y.value.length<10) y.value = "0" + y.value;
}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #5
Evertjan,

Pure Genius,

It is people like you that us beginner to intermediate programmers can
look upto, because you are willing to assist and even help complete
small tasks.

I am very grateful for your fast and excellent code support.

Thank You
David.

On the off chance, do you have any experience with MySQL ?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #6
David Gordon wrote on 04 jan 2005 in comp.lang.javascript:
On the off chance, do you have any experience with MySQL ?


Sorry, no.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #7
JRS: In article <11*********************@c13g2000cwb.googlegroups. com>,
dated Tue, 4 Jan 2005 04:58:39, seen in news:comp.lang.javascript,
da***@scene-double.co.uk posted :
THE PROBLEM:

If a user enters their 10 digit serial number, the first 4 digita are
month and year, (mmyy), i.e. 0105 123 456, then TB2 displays the
correct result but WITHOUT THE LEADING ZERO ?

How can I correct my code to leave the leading zero ?
Appreciate your help

You should have read the newsgroup FAQ.

Your serial number enters as a String; you perform arithmetic on it, so
you now have a Number. When javascript converts that to a String, it
has no reason to use a leading zero.

In your case, ISTM that you need to add at most one zero, and you will
need to add it if the Number is less than 10000 or 1000000000. You can
do that test, or test the length of the String after conversion, and
prepend "0" id required.

The general case of adding leading characters is covered in FAQ 4.6,
function Stretch.

Another approach would be to add a sufficiently large number like
10000000000, convert to String, and remove the first character.

Your system should have been designed to use not mmyy but yyyymm - such
problems then would not arise for about 7994 years.

See below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: \Dandy\ Randy | last post by:
Hello everyone. I have been following misc posts, as well as reading several FAQ's on this issue, unfortunatley I cannot locate a solution. I am hoping that someone will be able to provide me with...
6
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10...
28
by: Brent Eamer | last post by:
function SetDefaultDate() { d = new Date(); return d; } ........ <TD align=left> Start Date: </TD> <TD align=left> <SELECT name="batchStartDate" size="1" maxlength="50"...
1
by: Joshua Ammann | last post by:
Hello, I'm trying to export a query containing contact information, including a field. Some zip codes have one or two leading zeros, for example, San Juan, PR (00927) and Springfield, MA...
5
by: OneDay | last post by:
I've got a field that has some old data with text in it, but all forward data will be a 3 digit number. But many of the numbers are still only 2 digits. I would like to force the leading zero in...
5
by: GarryJones | last post by:
I have code numbers in 2 fields from a table which correspond to month and date. (Month, Code number) Field name = ml_mna 1 2 3 etc up to 12 (Data is entered without a leading zero)
6
by: Marko | last post by:
Hello all, I hope someone can help me; if got a value in a cell (040 1234567), when I run a query in the Analyser I got as respons only 40 1234567, so missing the zero (0) not the whole number...
8
by: Andrew Poulos | last post by:
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a positive integer, that is less than 100, by 100 I get a leading zero in front of the decimal point. For example 80/100 gives...
4
by: bobm2005 | last post by:
Whatever format I try in Printf, an 'E' format number nearly always has a leading non-zero:- 1.2345E7 -9.3456E8 etc. Is it possible to force it (printf) always to have leading zero? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.