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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • david@scene-double.co.uk

    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.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.

  • Evertjan.

    #2
    Re: Missing LEADING ZERO with script....pleas e help.

    wrote on 04 jan 2005 in comp.lang.javas cript:
    [color=blue]
    > 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[/color]

    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)

    Comment

    • David Gordon

      #3
      Re: Missing LEADING ZERO with script....pleas e help.

      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!

      Comment

      • Evertjan.

        #4
        Re: Missing LEADING ZERO with script....pleas e help.

        David Gordon wrote on 04 jan 2005 in comp.lang.javas cript:
        [color=blue]
        > 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.[/color]


        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)

        Comment

        • Evertjan.

          #5
          Re: Missing LEADING ZERO with script....pleas e help.

          Evertjan. wrote on 04 jan 2005 in comp.lang.javas cript:
          [color=blue]
          > 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;
          >}[/color]

          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)

          Comment

          • David Gordon

            #6
            Re: Missing LEADING ZERO with script....pleas e help.

            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!

            Comment

            • Evertjan.

              #7
              Re: Missing LEADING ZERO with script....pleas e help.

              David Gordon wrote on 04 jan 2005 in comp.lang.javas cript:
              [color=blue]
              > On the off chance, do you have any experience with MySQL ?[/color]

              Sorry, no.

              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • Dr John Stockton

                #8
                Re: Missing LEADING ZERO with script....pleas e help.

                JRS: In article <1104843519.829 210.10560@c13g2 000cwb.googlegr oups.com>,
                dated Tue, 4 Jan 2005 04:58:39, seen in news:comp.lang. javascript,
                david@scene-double.co.uk posted :[color=blue]
                >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[/color]


                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.

                Comment

                Working...