jsp中的信用卡类型和号码验证

时间:2017-08-28 06:54:17

标签: javascript jsp

我想运行一个与输入数字相匹配的信用卡类型的程序,即当我从select中选择卡片类型时,如果卡片类型与输入的数字匹配,则应加载卡片的图像。我使用过切换器使用if和else case的情况。当我只运行代码时,其他部分正在运行。

我需要的是它应该显示警告信息作为有效卡和卡的图像应该显示,如果不是它应该提醒作为无效卡。这是我的代码在下面..

<script type="text/javascript">
    		function validate() 
                {
    			var a = document.getElementById("mySelect").value;
    			var b = document.getElementById("cardno").value;
    			var v = /^(?:4[0-9]{12}(?:[0-9]{3})?)$/;   
    			var e = /^(?:3[47][0-9]{13})$/;   
    			var j = /^(?:(?:2131|1800|35\d{3})\d{11})$/;
    			var element = document.getElementById("img1");
    			var element1 = document.getElementById("img2");
    			var element2 = document.getElementById("img3");
    			var card=a;
    			switch(card){
    			case "visa":
    			if (b == v) 
                {
                alert("valid");
                element.src="images/visa.jpg";
    				
    			} else {
    				
    				alert("Invalid card number,Should start with 4");
    	
    			}
                break;
    			case "ae":
    			if (b == e) {
                    alert("valid");
    				element1.src="images/ae.jpg";
    			} else {
    				alert("Invalid card number,Should start with 34 or 37 ");
    			}
    			break;
    			case "jcb":
    			if (b == j) {
                    alert("valid");
    				element2.src="images/jcb.jpg";
    			} else
    			{
    				alert("Invalid card number,Should start with 2131
or 1800");
    			}
    			break;
    			default:
    				alert("Enter valid card numbers");
    			}
    		}
    	</script>
<html>
    <head>
    <title>User Form</title>
    </head>
    <body style="background-color: palegreen">
    <div align="left" height="50%" width="40%">
<form action="user" method="post">
    			<table>
    				<tr>
    					<td><label>UserName:</label></td>
    					<td><input type="text" name="uname"
    						placeholder="Enter username" required><br> <br></td>  
    				</tr>
    				<tr>
    					<td><label>cardname:</label></td>
    					<td><select id="mySelect">
    							<option value="">SelectCard</option>
    							<option value="visa">Visa</option>
    							<option value="ae">AmericanExpress</option>
    							<option value="jcb">Jcb</option>
    					</select><br> <br></td>
    				</tr>
    				<tr>
    					<td><label>cardnumber:</label></td>
    					<td><input type="text" name="nom" id="cardno" maxlength="16"
    						pattern="/^[0-9]+$/" title="Enter Valid Card No"
    						placeholder="Enter Card number" onkeypress="validate()" required><br>
    						<br></td>
    				</tr>
    				<tr>
    					<td><label>Date Of Expiry:</label></td>
    					<td><input type="text" name="doe" placeholder="dd/MM/yyyy"
    						required><br> <br></td>
    				</tr>
    				<tr>
    					<td><label>Bill Due Date:</label></td>
    					<td><input type="text" name="dat" placeholder="dd/MM/yyyy"
    						required><br> <br></td>
    				</tr>
    				<tr>
    					<td><input type="submit" value="Add"
    						style="height: 30px; width: 100px" class="glowing-border"></td>
    				</tr>
    			</table>
    		</form>
    	</div> 
    	<img src="images/visa.jpg" id="img1" align="right" height="40"
    		width="50">
    	<img src="images/ae.jpg" id="img2" align="right" height="40" width="50">
    	<img src="images/jcb.jpg" id="img3" align="right" height="40"
    		width="50">
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

您正在将RegExp个对象与不起作用的字符串进行比较 我猜你想用正则表达式测试字符串。这不是通过比较器(== || ===)完成的,而是通过调用regexp对象上的test方法将字符串传递给test作为参数,如下所示:

let regexpObj = /^(?:4[0-9]{12}(?:[0-9]{3})?)$/;
if (regexpObj.test("a string")) {
  // If the method returns true, its a match, else its not a match.
}

我建议您阅读有关正则表达式的MDN web docs