Solved

Form variable value is being added on to previous value in a text field

Posted on 2001-06-30
13
262 Views
Last Modified: 2008-03-06
Hi, this may be an easy question, but I put medium since I pasted in so much code.

I haven't run into this before, I've been using this technique a lot with no problem.

(1) A user enters a credit card number in orderform2.asp and submits to orderform3.asp
(2) orderform3.asp uses the Luhn algorithm to see if the credit card number is a valid, and if not runs the errorForm method, which passes all the values back to orderform2.asp.
(3) My problem is that when I type new info into the ccnum text field in orderform2.asp, the value is added on to the previous value, instead of replacing it.  And if I keep trying to submit orderform2.asp and return from orderform3.asp, the values keep getting added on to each other, so pretty soon my ccnum field is a string containing a gazillion characters.

Anybody know why this is happening?  I thought about forcing the browser to bypass its cache, but I don't think that's what the problem is.

Below is the relevant code for orderform1.asp and orderform2.asp:

orderform2.asp
<%ccnum= TRIM( Request.Form ( "ccnum" ) )%>
<FORM METHOD="POST" ACTION="orderform3.asp">
<input name="ccnum" size=16 value="<%=Server.HTMLEncode(ccnum)%>">
</FORM>

orderform3.asp
<%
SUB errorForm ( theError)
%>

<HTML>
<HEAD><TITLE>Error</TITLE></HEAD>
<BODY>
<%=theError%>
<FORM method="post" ACTION="orderform2.asp">
<% FOR EACH item IN Request.Form%>
<INPUT name ="<%=item%>" type="hidden" value="<%=Server.HTMLEncode(Request.Form( item ) )%>">
<% NEXT
%>
  <p>
    <INPUT TYPE="submit" value="Return">
</FORM>
</BODY>
</HTML>
<%
Response.End
END SUB

FUNCTION cleanCCNum(ccnumber)

     FOR i = 1 TO Len(ccnumber)
          IF isNumeric(MID(ccnumber, i, 1)) THEN
               cleanCCNum=cleanCCNum & MID(ccnumber, i, 1)
          END IF
     NEXT
Response.Write(cc)
END FUNCTION

FUNCTION validCCNumber (ccnumber)
        ccnumber = cleanCCNum(ccnumber)
        IF ccnumber = "" THEN
           validCCNumber = FALSE
        ELSE
        isEven = False
        digits = ""
        FOR i = Len(ccnumber) TO 1 STEP -1
     IF isEven THEN
        digits = digits & CINT(MID(ccnumber, i, 1))*2
     ELSE
        digits = digits & CINT(MID(ccnumber, i, 1))
     END IF
     isEven = (Not isEven)
     NEXT
     checkSum = 0
     FOR i = 1 TO Len(digits) STEP 1
        checksum = checkSum + CINT(MID(digits, i, 1))
     NEXT
     validCCNumber = ((checkSum Mod 10) = 0)
     END IF
END FUNCTION


cc= TRIM( Request.Form ( "ccnum" ) )


IF NOT validCCNumber(cc) THEN
   errorForm "The credit card number you entered is not valid.  Please try again"
END IF



Thanks in advance for any help,

Travis
0
Comment
Question by:starbuck111
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 7

Expert Comment

by:weesiong
ID: 6241799
starbuck111,

From a good shopping cart, using the cyber bank to validation, no using script, script canot detail tell you the Card Number and Expires Day is validation or not, and no store the use Credit Card in the database. :)

Regards,
Wee Siong
0
 

Author Comment

by:starbuck111
ID: 6241923
Thanks,

I am signed up with a cyber bank, I just wanted to check to see that the numbers formed a valid algorithm first before I sent the info off to the bank.

0
 
LVL 7

Expert Comment

by:weesiong
ID: 6242372
0
 
LVL 5

Expert Comment

by:Yog
ID: 6243486
here is a script you can use before sending ...

---------

<!-- TWO STEPS TO INSTALL CREDIT CARD VALIDATION:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Simon Tneoh (tneohcb@pc.jaring.my) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var Cards = new makeArray(8);
Cards[0] = new CardType("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
Cards[1] = new CardType("VisaCard", "4", "13,16");
var VisaCard = Cards[1];
Cards[2] = new CardType("AmExCard", "34,37", "15");
var AmExCard = Cards[2];
Cards[3] = new CardType("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
Cards[4] = new CardType("DiscoverCard", "6011", "16");
var DiscoverCard = Cards[4];
Cards[5] = new CardType("enRouteCard", "2014,2149", "15");
var enRouteCard = Cards[5];
Cards[6] = new CardType("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard = Cards[6];
var LuhnCheckSum = Cards[7] = new CardType();

/*************************************************************************\
CheckCardNumber(form)
function called when users click the "check" button.
\*************************************************************************/
function CheckCardNumber(form) {
var tmpyear;
if (form.CardNumber.value.length == 0) {
alert("Please enter a Card Number.");
form.CardNumber.focus();
return;
}
if (form.ExpYear.value.length == 0) {
alert("Please enter the Expiration Year.");
form.ExpYear.focus();
return;
}
if (form.ExpYear.value > 96)
tmpyear = "19" + form.ExpYear.value;
else if (form.ExpYear.value < 21)
tmpyear = "20" + form.ExpYear.value;
else {
alert("The Expiration Year is not valid.");
return;
}
tmpmonth = form.ExpMon.options[form.ExpMon.selectedIndex].value;
// The following line doesn't work in IE3, you need to change it
// to something like "(new CardType())...".
// if (!CardType().isExpiryDate(tmpyear, tmpmonth)) {
if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
alert("This card has already expired.");
return;
}
card = form.CardType.options[form.CardType.selectedIndex].value;
var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval)



// comment this out if used on an order form
alert("This card number appears to be valid.");


else {
// The cardnumber has the valid luhn checksum, but we want to know which
// cardtype it belongs to.
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkCardNumber(form.CardNumber.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getCardType();
break;
   }
}
if (cardname.length > 0) {
alert("This looks like a " + cardname + " number, not a " + card + " number.");
}
else {
alert("This card number is not valid.");
      }
   }
}
/*************************************************************************\
Object CardType([String cardtype, String rules, String len, int year,
                                        int month])
cardtype    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cardnumber, eg: "4", "6011", "34,37".
len         : valid length of cardnumber, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new CardType("Visa", "4", "16");
var AmExCard = new CardType("AmEx", "34,37", "15");
\*************************************************************************/
function CardType() {
var n;
var argv = CardType.arguments;
var argc = CardType.arguments.length;

this.objname = "object CardType";

var tmpcardtype = (argc > 0) ? argv[0] : "CardObject";
var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

this.setCardNumber = setCardNumber;  // set CardNumber method.
this.setCardType = setCardType;  // setCardType method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

this.setCardType(tmpcardtype);
this.setLen(tmplen);
this.setRules(tmprules);
if (argc > 4)
this.setExpiryDate(argv[3], argv[4]);

this.checkCardNumber = checkCardNumber;  // checkCardNumber method.
this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
this.getCardType = getCardType;  // getCardType method.
this.isCardNumber = isCardNumber;  // isCardNumber method.
this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

/*************************************************************************\
boolean checkCardNumber([String cardnumber, int year, int month])
return true if cardnumber pass the luhncheck and the expiry date is
valid, else return false.
\*************************************************************************/
function checkCardNumber() {
var argv = checkCardNumber.arguments;
var argc = checkCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setCardNumber(cardnumber);
this.setExpiryDate(year, month);

if (!this.isCardNumber())
return false;
if (!this.isExpiryDate())
return false;

return true;
}
/*************************************************************************\
String getCardType()
return the cardtype.
\*************************************************************************/
function getCardType() {
return this.cardtype;
}
/*************************************************************************\
String getExpiryDate()
return the expiry date.
\*************************************************************************/
function getExpiryDate() {
return this.month + "/" + this.year;
}
/*************************************************************************\
boolean isCardNumber([String cardnumber])
return true if cardnumber pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function isCardNumber() {
var argv = isCardNumber.arguments;
var argc = isCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
if (!this.luhnCheck())
return false;

for (var n = 0; n < this.len.size; n++)
if (cardnumber.toString().length == this.len[n]) {
for (var m = 0; m < this.rules.size; m++) {
var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
if (headdigit == this.rules[m])
return true;
}
return false;
}
return false;
}

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else return false.
\*************************************************************************/
function isExpiryDate() {
var argv = isExpiryDate.arguments;
var argc = isExpiryDate.arguments.length;

year = argc > 0 ? argv[0] : this.year;
month = argc > 1 ? argv[1] : this.month;

if (!isNum(year+""))
return false;
if (!isNum(month+""))
return false;
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

/*************************************************************************\
boolean luhnCheck([String CardNumber])
return true if CardNumber pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

var CardNumber = argc > 0 ? argv[0] : this.cardnumber;

if (! isNum(CardNumber)) {
return false;
  }

var no_digit = CardNumber.length;
var oddoeven = no_digit & 1;
var sum = 0;

for (var count = 0; count < no_digit; count++) {
var digit = parseInt(CardNumber.charAt(count));
if (!((count & 1) ^ oddoeven)) {
digit *= 2;
if (digit > 9)
digit -= 9;
}
sum += digit;
}
if (sum % 10 == 0)
return true;
else
return false;
}

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
this.size = size;
return this;
}

/*************************************************************************\
CardType setCardNumber(cardnumber)
return the CardType object.
\*************************************************************************/
function setCardNumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}

/*************************************************************************\
CardType setCardType(cardtype)
return the CardType object.
\*************************************************************************/
function setCardType(cardtype) {
this.cardtype = cardtype;
return this;
}

/*************************************************************************\
CardType setExpiryDate(year, month)
return the CardType object.
\*************************************************************************/
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}

/*************************************************************************\
CardType setLen(len)
return the CardType object.
\*************************************************************************/
function setLen(len) {
// Create the len array.
if (len.length == 0 || len == null)
len = "13,14,15,16,19";

var tmplen = len;
n = 1;
while (tmplen.indexOf(",") != -1) {
tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
n++;
}
this.len = new makeArray(n);
n = 0;
while (len.indexOf(",") != -1) {
var tmpstr = len.substring(0, len.indexOf(","));
this.len[n] = tmpstr;
len = len.substring(len.indexOf(",") + 1, len.length);
n++;
}
this.len[n] = len;
return this;
}

/*************************************************************************\
CardType setRules()
return the CardType object.
\*************************************************************************/
function setRules(rules) {
// Create the rules array.
if (rules.length == 0 || rules == null)
rules = "0,1,2,3,4,5,6,7,8,9";
 
var tmprules = rules;
n = 1;
while (tmprules.indexOf(",") != -1) {
tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
n++;
}
this.rules = new makeArray(n);
n = 0;
while (rules.indexOf(",") != -1) {
var tmpstr = rules.substring(0, rules.indexOf(","));
this.rules[n] = tmpstr;
rules = rules.substring(rules.indexOf(",") + 1, rules.length);
n++;
}
this.rules[n] = rules;
return this;
}
//  End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<center>
<form name="ThisForm">
Card Number: <input name="CardNumber" size="16" maxlength="19"><br>
Card Type:
<select name="CardType">
<option value="MasterCard">MasterCard
<option value="VisaCard">Visa
<option value="AmExCard">American Express
<option value="DinersClubCard">Diners Club
<option value="DiscoverCard">Discover
<option value="enRouteCard">enRoute
<option value="JCBCard">JCB
</select>
<br>
Expiration Date: Month
<select name="ExpMon">
<option value="1" selected>1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
<option value="11">11
<option value="12">12
</select>
Year <input name="ExpYear" size="2" maxlength="2">(Range: 1997~2020)<br>
<input type="button" value="Check" OnClick="CheckCardNumber(this.form)"><br>
</form>
</center>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size:  12.38 KB -->

cheers.
0
 

Author Comment

by:starbuck111
ID: 6243502
Thanks, everyone, I really appreciate the responses, but I'm not actually asking about the Luhn check, or how to check credit card numbers.  The problem is this:

1.  I enter a value into a textfield on orderform2.asp, and submit to orderform3.asp
2.  There is a problem on orderform3.asp, so I bring up a form which submits back to orderform2.asp
3.  I type in a new value into the textfield on orderform2.asp
4.  When I resubmit to orderform3.asp, the value I just typed in is added on to the previous value, instead of replacing it.  To me this is weird, and I don't know why its happening, I haven't run into it before.

Thanks for all the help,

Travis
0
 
LVL 5

Expert Comment

by:Yog
ID: 6243513
1. How do you pass back , do you have any hidden fields to pass back. Posting the code might help to point
2. Check if your page is caching.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:Yog
ID: 6243515
yeah i guess in the final case, you might have a hideen field with the same name, and a text field, which will be taken as a array of textbox like thing and previous and both will be appended. need to revamp your code a bit..adding some if conditions lol..
0
 
LVL 7

Expert Comment

by:weesiong
ID: 6243521
starbuck111,

Ok, i got what your means now, add
<%
Response.Expires = 0 'in asp header
%>

And tell me how you go back the form2?

Regards,
Wee Siong
0
 

Author Comment

by:starbuck111
ID: 6244115
Hi, I tried Response.Expires=0, but unfortunately it didn't fix the problem.

I'm passing the values back in hidden form fields, all the info is at the very top, here's the code I use to do it.

<% FOR EACH item IN Request.Form%>
<INPUT name ="<%=item%>" type="hidden" value="<%=Server.HTMLEncode(Request.Form( item ) )%>">
<% NEXT

I thought Yog's explanation of an array textbox thing makes sense, I've tried some experimentation, I'll keep playing with it.

I noticed one new thing, though if this helps.  As the values keep getting added on to each other, it's adding commas between the values.  So if the first one I try is "aaaaa" and the second one I try is "bbbbbb", it comes out as aaaaa, bbbbb

I've been using this hidden form fields a lot though, and never had a problem.  I'm obviously not an .asp expert, but I did take a couple of classes, and I've never heard of anything being wrong with this method of passing hidden form fields back to textboxes.

Anyway, sorry to drive you all crazy with this stuff.  I think all the relevant code is on the very first post.

Travis
0
 

Author Comment

by:starbuck111
ID: 6244122
One other thing, that might be of help.

In orderform2.asp, I tried setting the value of the textbox to "" using straight HTML code.  Then when I submit to orderform3.asp, the new value is still just added to whatever values I had typed in previously.  This makes me think that the problem is in orderform3.asp.

Thanks,

Travis
0
 
LVL 5

Expert Comment

by:Yog
ID: 6244234
hi travis

 no there is not a problem , form behaviour is like that , if you have 2 fields with same name it will be like an array and can get it in server side as a comma seperated value, and the client side as
firlaname[0], fieldname[1], likewise in javascript if you need.

providing more details on the orderform2.asp may help to solve the problem ...
0
 
LVL 7

Accepted Solution

by:
John844 earned 100 total points
ID: 6244532
your problem appears to be here

<% FOR EACH item IN Request.Form%>
<INPUT name ="<%=item%>" type="hidden" value="<%=Server.HTMLEncode(Request.Form( item ) )%>">
<% NEXT
%>

You write out hidden values for all items that came in the form post, then later in your page, you add all of the form input fields again. When your page posts the second time, you get firstValue, SecondValue for every control.  each time you submit the page, another item will get added to the list.

You need to remove the loop adding the hidden fields.  Only add fields that are not displayed directly on the web page for users to input information into.  If the field is displayed on the page, prefill the value with whatever came across in the form post.

If you need help with any of these issues, let us know.

John
0
 

Author Comment

by:starbuck111
ID: 6247278
OK, now I understand.  Once I did a view source on each form, I found out that I now had 2 fields of the same name - 1 hidden and one and actual text field.  Thanks for all the help.  I wasn't sure who to give the points to, because Zog was right, but John made it clear to me what I was doing wrong, but it's not about the points after all, is it?

Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now