?
Solved

ASP: Javascript function ignoring array values with leading zeroes.

Posted on 2004-10-11
3
Medium Priority
?
262 Views
Last Modified: 2012-06-27
Hello experts...

I am having a problem with the following function.  The purpose is to make sure that the number entered in the "Alt_StoreNum" field is included in the array.  Store numbers in the SQL database do not include leading zeroes.  I want them to be able to enter leading zeroes, but not require it.  Is there an easier way to do this?

The array is generated from a SQL table.  When parsing the recordset data into the array, I am manually including leading zeroes via the following script:

storeray = "("
while not rsStores.eof
storeray = storeray&rsStores("StoreNum")&","
if rsStores("StoreNum") < 10 then storeray = storeray&"00"&rsStores("StoreNum")&","
if rsStores("StoreNum") > 9 AND rsStores("StoreNum") < 100 then storeRay = storeray & "0"&rsStores("StoreNum")&","
rsStores.moveNext
wend
storeray = Left(StoreRay,(len(StoreRay)-1))
storeray = storeray&")"

Here is teh script output.  

var aList = new Array(1,001,3,003,5,005,7,007,8,008,10,010,11,011,14,014,15,015,19,019,21,021,22,022,23,023,27,027,28,028,29,029,31,031,32,032,33,033,34,034,35,035,38,038,40,040,41,041,43,043,45,045,46,046,48,048,49,049,50,050,51,051,52,052,55,055,57,057,58,058,59,059,60,060,61,061,62,062,63,063,64,064,65,065,66,066,68,068,69,069,70,070,72,072,73,073,74,074,75,075,76,076,79,079,81,081,82,082,84,084,85,085,86,086,87,087,89,089,90,090,92,092,96,096,99,099,100,101,113,114,115,116,121,122,123,124,126,127,128,129,130,131,132,133,135,136,137,139,141,143,144,145,146,147,148,149,150,151,152,153,154,155,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,359,361,365,366,701,750);
    function CheckVal()
    {
        var strValue = frmAddOrder.alt_storenum.value;

        var blnValOK = false;
        if (strValue == "") {
            // Blank is okay
            blnValOK = true;
        }
        else if('' + parseInt(strValue) != strValue) {
            // Not an integer
            blnValOK = false;
        }
        else {
            // Check against array
            blnValOK = false;
            var intValue = parseInt(strValue);
            for (var i = 0; i < aList.length; i++)
                if(aList[i] == intValue)
                    blnValOK = true;
        }

        if(!blnValOK)
            alert(strValue + ' is not a valid store number')
                  
         return blnValOK;
         }


Here is the section of the form that calls this function:

<table width="95%" border="0" cellpadding="0" cellspacing="3">
      <tr>
              <td class="formLabel">Store Number:</td>
            <td class="pagetext"><strong>1</strong></td>
            <td class="formLabel">Store Contact:</td>
            <td><INPUT NAME="contact" TYPE="TEXT" ID="contact"   SIZE="20" onKeyPress="var k=event.keyCode;if(!(k>=65&&k<=122)&&k!=32){return false;}"></td>
      </tr>
                  
      
            <tr>
            <td class="formLabel">If ordering for another store, enter store number: </td>
            <Td><input type="text" name="alt_storenum" id="alt_storenum" size="4" onBlur="CheckVal()" onKeyPress="var k=event.keyCode;if(!(k>=48&&k<=57)){return false;}"></Td>
            <td class="formLabel">Phone Number:</td>
            <td class="pagetext" align="left">111-111-1111<INPUT TYPE="HIDDEN" NAME="phone" SIZE="10" VALUE="111-111-1111"></td></tr><tr><td align="right" class="formlabel">Employee Number:<br>
(required if ordering from a different store)</td>
              <td align="left"><input name="empNum" type="text" size="8" onKeyPress="var k=event.keyCode;if(!(k>=48&&k<=57)){return false;}"></td></tr></table>
0
Comment
Question by:boudog13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

by:Thogek
ID: 12280831
One thing I'd do is change
    if rsStores("StoreNum") > 9 AND rsStores("StoreNum") < 100 then storeRay = storeray & "0"&rsStores("StoreNum")&","
to the simpler
    if rsStores("StoreNum") < 100 then storeRay = storeray & "0"&rsStores("StoreNum")&","

That way, instead of entering
    1,001
into the array, you should get
    1,001,01
which accounts for whether the user leads with one zero or two.
0
 
LVL 15

Accepted Solution

by:
Thogek earned 300 total points
ID: 12280871
Also, note that this block

    storeray = storeray&rsStores("StoreNum")&","
    if rsStores("StoreNum") < 10 then storeray = storeray&"00"&rsStores("StoreNum")&","
    if rsStores("StoreNum") > 9 AND rsStores("StoreNum") < 100 then storeRay = storeray & "0"&rsStores("StoreNum")&","

which only writes numbers to the array.  And 001 = 01 = 1, no matter how you write it, so you just end up with multiple values of the array with the same numeric value.

If you want to account for leading zeroes this way, you'll need to convert these elements to strings, like so:

    storeray = """" & storeray & rsStores("StoreNum") & ""","
    if rsStores("StoreNum") < 10 then storeray = """" & storeray & "00" & rsStores("StoreNum") & ""","
    if rsStores("StoreNum") > 9 AND rsStores("StoreNum") < 100 then storeRay = """" & storeray & "0" & rsStores("StoreNum") & ""","

and then change

        else {
            // Check against array
            blnValOK = false;
            for (var i = 0; i < aList.length; i++)
                if(aList[i] == strValue)
                    blnValOK = true;
        }

to

        else {
            // Check against array
            blnValOK = false;
            var intValue = parseInt(strValue);
            for (var i = 0; i < aList.length; i++)
                if(aList[i] == strValue)
                    blnValOK = true;
        }
0
 
LVL 14

Assisted Solution

by:steveberzins
steveberzins earned 200 total points
ID: 12391595
why make the test array bigger than it has to be, it looks like all you care is that the numeric value is valid.

but beware, parseInt can be probematic where, for example, '08' or '09' are not parsed to 8 or 9 like a normal person would think...

use parseInt(number, 10) to parse the value as a base 10 number.

Then I'd just build the array with actual values to keep it small, and allow them to type whatever they wan't,
all you care is the numeric value of what they entered is valid right?

So don't worry about the string representation variations.

and use the code you have, which should work just fine with the small tweak of using the radix parameter of parseInt.

var intValue = parseInt(strValue,10);
for (var i = 0; i < aList.length; i++) {
   if(aList[i] == intValue) {
       blnValOK = true;
   }
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question