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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select List validation

Status
Not open for further replies.

dougrun

Programmer
Mar 8, 2001
3
US
I have 2 list boxes (month, year) for a credit card expiration date. I want a simple if..then statement to check if the card is expired. Here is what I have now:

if (document.basketform.ccexp.options[document.basketform.ccexp.selectedIndex].value == '01') && (document.basketform.year.options[document.basketform.year.selectedIndex].value == '01') {
rc=alert('Your Card has Expired!');
return false;
}

but it doesn't work. ccexp and year are the two names. I suppose having ti check against the actual date would be fancier but I'd settle for this. Any help is appreciated and worth a beer if you're in San Luis Obipos, CA .
 
I used to know someone who lived there - Do you know Kerri McCallum? I don't understand the code you have there, you have a select box for expiry date right? So why do have the user select the current year? This could be prone to errors could it not? I think you should compare it to the current date like:
<script>

function checkExp(select){
var dateObj = new Date();
var currentYear = dateObj.getYear();

if(select.options[select.selectedIndex].value == currentYear){
alert(&quot;Card has expired!&quot;)
}
else{/*card is good */}
}
</script>

<head>
<title>Some kinda order form</title>
</head>
<body>
<form>
<select name=&quot;ccexp&quot; size=1 onChange=&quot;checkExp(this)&quot;>
<option value=null>
Please select expiry date
</option>
<option value='2001'>
01
</option>
<option value='2002'>
02
</option>
</select>
</form>


Apologies if I have the wrong end of the stick! I don't have a credit card so I am not exactly sure of the context :-Q
b2 - benbiddington@surf4nix.com
 
Hi dougrun!
Let's see what we have here:

if (statement) && (statement)
....

there is an error here and correct one is this:

if ( (statement) && (statement) )

I'm quite sure that this is the major problem of your script.

Andrew | starway@mail.com
 
Thanks for the help guys. I did catch the double (()) right after I posted and it still didn't work. In fact, if I leave it in my page, it screws up my other javascript shopping cart. The page is checkout page of .

Is it ok to have the '01' like it is or do I have to refer to it as what # it is in the list somehow?
 
i fixed it!! the correct line is:

if ((document.basketform.ccexp.selectedIndex == 0) && (document.basketform.year.selectedIndex == 0)) {
rc=alert('Your Card has Expired!');
return false;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top