View Single Post
Old 12-04-2010, 03:40 AM   PM User | #3
hotwheelharry
Regular Coder

 
Join Date: Jun 2008
Posts: 102
Thanks: 6
Thanked 9 Times in 9 Posts
hotwheelharry is an unknown quantity at this point
You speeled s0me th!ngz wrawng.

Here you go...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Credit Card Validation</title>
<link rel="stylesheet" type="text/css" href="default.css" />
<script type="text/javascript">

function checkCC(myForm)
{
	var ccType;
	var ccLength;
	var ccNum = myForm.cardNum.value; 
	if (myForm.cardType.selectedIndex == 0)
	{
		ccType = "Visa";
	}
	
	else if (myForm.cardType.selectedIndex == 1)
	{
		ccType = "MasterCard"
	}
	
	else if (myForm.cardType.selectedIndex == 2)
	{
		ccType = "AmericanExpress"
	}
	
	 switch(ccType)
    {
		case "Visa":
		{
        	valid = /^4\d{12}(?:\d{3})?$/;
			
			if (valid.test(myForm.cardNum.value))
			{
				alert("This is a Valid Visa Card");
				return true;
			}
			
			else
			{
				alert("This Card Number is Invalid For Visa");
				return false;
			}
        	break;
		}
		
      	case "MasterCard":
		{
        	  	valid = /^5[1-5]\d{14}$/;
			
			if (valid.test(myForm.cardNum.value))
			{
				alert("This is a Valid MasterCard Card");
			}
			
			else
			{
				alert("This Card Number is Invalid For MasterCard");
			}
        	break;
		}

      	case "AmericanExpress":
		{
        	valid = /^3[47]\d{13}$/;
			
			if (valid.test(myForm.cardNum.value))
			{
				alert("This is a Valid American Express Card");
				//return true;
			}
			
			else
			{
				alert("This Card Number is Invalid For American Express");
				//return false;
			}
        	break;
		}

      	default:
        	myForm.cardNum.value = null;
        	alert("Card type not found");
			return false;
    }
}
</script>
</head>

<body>

<h1>Credit Card Validator</h1>
<table>
<form name="creditCard">

<tr>
	<td><span class="labels">Card Type:</span></td>

	<td>
		<select name="cardType" size="3">
			<option name="visa">Visa</option>
    		<option name="mc">Master Card</option>
    		<option name="amex">American Express</option>
		</select>
   	</td>
    
<tr>
	<td><span class="labels">Card Number:</span></td>
	<td><input name="cardNum" type="text" size=30 value="" /></td>
</tr>

<tr><td colspan="2">&nbsp;</td></tr>

<tr>
	<td>
	<input type="button" value="Death to All" 
	 style="background-color:#666666 !important"
	 onclick="return(checkCC(this.form))" />
    </td>
</tr>
</form>
</table>

</body>
</html>
hotwheelharry is offline   Reply With Quote
Users who have thanked hotwheelharry for this post:
ParadoxKing (12-06-2010)