Solved

ASP: Javascript function ignoring array values with leading zeroes.

Posted on 2004-10-11
3
257 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
  • 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 75 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 50 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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