473,699 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Missing LEADING ZERO with script....pleas e 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.queryst ring("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 2894
wrote on 04 jan 2005 in comp.lang.javas cript:
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.javas cript:
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.javas cript:
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.valu e)||(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.javas cript:
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************ *********@c13g2 000cwb.googlegr oups.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
4261
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 the simple answer. My problem has to do with the leading white spaces after the first line when calling data using the @ variable. Here is my code: open (PREVIEW, "<preview.txt") or &error("Unable to open the data file for reading"); flock...
6
13781
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 digits), the first 4 being the month and year (mmyy)
28
1800
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" value="<SCRIPT>SetDefaultDate()</SCRIPT>"> </TD> </TR>
1
12776
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 (01104). When I export to a comma separated value (.csv) file, the zip code for San Juan becomes "927" and for Springfield becomes "1104". How can I prevent the leading zero(s) from being trimmed in the exported .csv file? Because the table contains...
5
20410
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 the entry of the field. For example if the number 77 is entered into the field, 077 will display. How do I format to force the leading zero?
5
3474
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
1819
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 is displayed. When I run a query on a cell with value 1234567 I received the number 1234567 and that's oke. The Data Type of the Column is Char. Thanks,
8
4819
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 0.8 This is what the server app is expecting a leading zero (it does indeed fail without it). Do I need to do anything to ensure the leading zero is there?
4
3214
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? Thus, the above becomes:-
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.