Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form data validation 2

Status
Not open for further replies.

gchen

Programmer
Nov 14, 2002
174
US
i did a quick search but no luck.... hope someone can shed some light for me here...

i am building a form to let user to submit some info and one of the form field is "coupon code" where i was required to add a validation to check if data entered for this field falls into a list of qualified coupon codes.

i am thinking it can be done by using a "in" phrase but i do not know eactly the sytan to make it work.

Any idea? Thanks a lot!
 
How many different coupon codes will be used - and will they always be the same or will they change from time to time?

There's always a better way. The fun is trying to find it!
 
i have about 20 codes. there is no pattern and these 20 codes pretty much the same for one particular event. so i am thinking using an arry to hold these 20 codes and check if form.coupon.value is in this array. if this makes sense, how do i construct the code, i need some help for the syntax.

thanks!
 
I would suggest that rather than try to do this validation client-side, do it on the server instead. This way, you can place the coupon codes in a database and just perform a search on the database. An additional benefit is that you'll never have to change your javascript code when one, or more, of the coupon codes change.

There's always a better way. The fun is trying to find it!
 
i couldn't agree with you more. but i do not have a server with database to work with and this is a small event, we might have 4 events a year. so i would need to construct 4 pages and 4 similar javascripts a year. the form actually is a form mial kind of implementation, so you know what i meant...

any suggestion on client side implementation? thanks!

 
Well, I suppose the easiest way would be to just use an "if" statement like this:

if (document.form.coupon_code.value != "abc123" || document.form.coupon_code.value != "xyz987" || document.form.coupon_code.value != "123abc" ||document.form.coupon_code.value != "987xyz")
{
alert("Invalid Coupon Code!");
document.form.coupon_code.focus();
return (false);
}

Or you could get a little more complex and create an array of coupon codes and the use a for loop to cycle through the array, but I think the "if" statement will be simpler and faster - or just as fast, anyway.



There's always a better way. The fun is trying to find it!
 
yeah, i was planning of using array and loop through it but i don't know exactly the javascript syntax. well, i think i can live with if statement... Thanks!
 
Okay, i managed to get below code working using array ...

<script>

var myCoupon = [11,22,33,44,55];
var myCount = myCoupon.length;
var i=0;

do {
CouponNumber = myCoupon;
CouponCode = "44";
if (CouponNumber == CouponCode) {
document.write("Got it "+ myCoupon + ", " + CouponCode +"<br>");
}
else {
document.write("Oops......"+"<br>");
};
i+=1;
} while (i<myCount);

</script>

i will just need to replace the hard coded CouponCode by form field and i am done!

hope this will help others.

 
Reference to form text field is so commonly known that I doubt this might help.
[tt]
var CouponCode = document.formname.textfieldname.value;
[/tt]
and put it outside of the do loop.
 
here it is...

<script>
function checkdata() {
if (document.form1.promotion.value.length == 0) {
alert ("Please enter Promotion Code.");
document.form1.promotioncode.select();
document.form1.promotioncode.focus();
return false;
}

var myCoupon = [ 111,222,333,444,555 ];
var myCount = myCoupon.length;
var CouponCode = document.form1.promotioncode.value;
var i=0;
do {
CouponNumber = myCoupon;
if (CouponCode == CouponNumber) {
msg = "";
break;
}
else {
msg = "Invalid Promotion Code!"
};
i+=1;
} while (i<myCount);
if ( msg.length > 0 ) {
alert(msg);
document.form1.custom1.select();
document.form1.custom1.focus();
return false;
}
return true;
}
</script>

any suggestion to trim the code?

thanks!
 
For stable ff/nn behavior, add structure.
[tt]
document.open();
//blah blah; document.write(...);
document.close();
[/tt]
 
what do you mean... ff/nn behavior...?
 
The structure is good no matter what browser you are using.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top