---
title: "文本域输入多个数据字符串验证（用反斜杠隔开号码）"
author: "coldg"
date: "2015-06-08T14:21:15Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2015-06-08T14:37:31Z"
id: 2217825
url: "https://coldg.iteye.com/blog/2217825"
---

要求：文本域输入多个卡号之间可以用空格换行符隔开，验证时把不符合纯数字和长度不合适的卡号直接过滤掉。代码如下:

```
function check(n) {   // 检测函数
 // 正则 表示 开头为数字 要11位 结尾
     var text = n.value;
     text = $.trim(text);
     if (text == "") {
          alert("请输入卡号");
          return false;
     }
     text = text.replace(/\r\n/g, "/");
	  text = text.replace(/\n/g, "/");
	  text = text.replace(/\s/g, "/");
	  var cards = text.split("/");
	   
	  var reg = /^[\d]{4,13}$/;     

	  for(var i=0;i<cards.length;i++){
	   if(cards[i]!="" && !reg.test(cards[i])) { 
		 console.info(cards[i]);
	         console.info(i+'卡号输入有误');
                 var temp = text.replace(cards[i],'');
	         text = temp;
	       } 
	   }
	   text = text.toString();
	   var reg1 = /[\\\/]/g;  
	   console.info(text);
	   text = text.replace(/\/+/g, "/");   //替换连续多个反斜杠为一个
	   document.getElementById("content").value = text; 
	 }
```

 效果图：  
![](http://dl2.iteye.com/upload/attachment/0109/3164/d7a9447a-714c-30be-bd73-adccc8ddaacf.png)       
   
                   
![](http://dl2.iteye.com/upload/attachment/0109/3166/5c16c196-8172-388e-9da3-8c81b5591578.png)