需要验证带有“xxx-xxxxxxx-yy”的输入字段

时间:2015-04-23 09:44:21

标签: jquery regex validation

我正在尝试验证数字格式,并且在输入时该字符应自动替换为字符。

格式为:xxx-xxxxxxx-yy AND在另一个字段“xx-xx-xx-xxx-xx”

我的代码:

$('#id_number').on("keypress keyup blur", function(event) {
        $('span.error-keyup-1').hide();
        var inputVal = $(this).val();
        $(this).val($(this).val().replace(/[^0-9\.]/g, ''));
        var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
        if(!numericReg.test(inputVal)) {
            $(this).after('<span class="error error-keyup-1">Numeric characters only.</span>');
        }
    });

我出现了,直到允许数字。但是在用户输入时用数字替换字符变得很困难。

有人可以帮助我。有没有正则表达式?

$(document).ready(function() {
	$('#id_number').on("keypress keyup blur", function(event) {
		$('span.error-keyup-1').hide();
		var inputVal = $(this).val();
		//$(this).val($(this).val().replace(/[^0-9\.]/g, ''));
		var numericReg = /^(?:\d{3}(?:-\d{7})?(?:-\d{2})?|\d{2}(?:(?:-\d{2}){0,2})?(?:-\d{3})?(-\d{2})?)$/gm;
		if(!numericReg.test(inputVal)) {
			$(this).after('<span class="error error-keyup-1">Numeric characters only, and pattern should match either "xxx-xxxxxxx-yy" or "xx-xx-xx-xxx-xx".</span>');
		}
	});
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="inp-wrap">
<input type="text" class="form-control" id="id_number" name="id_number" maxlength="20" required>
</div>

3 个答案:

答案 0 :(得分:2)

检查出来..另一种方法......并且完美无缺! :)

$(document).ready(function() {
	$('#id_number').on("keyup blur", function(event) {
		$('span.error-keyup-1').hide();
		var inputVal = $(this).val();
        var length = inputVal.length;
        var letters = /[^0-9-]/gm;
		
        if(letters.test(inputVal)) {
		$(this).val($(this).val().replace(/[^0-9-]/g, ''));
    }
        var buff = "xxxxxxxxxxxxxxx";
        inputVal = inputVal+buff;
        inputVal = inputVal.substr(0,15);
		var numericReg = /^([x\d]{3}[x-][x\d]{7}[x-][x\d]{3})$|^(([x\d]{2}[x-]){3}[x\d]{3}[x-][x\d]{2})$/gm;
		if(!numericReg.test(inputVal)) {
            $(this).val(inputVal.substr(0,length-1))
			$(this).after('<span class="error error-keyup-1">Numeric characters only, and pattern should match either "xxx-xxxxxxx-yy" or "xx-xx-xx-xxx-xx".</span>');
		}
	});
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="inp-wrap">
<input type="text" class="form-control" id="id_number" name="id_number" maxlength="20" required>
</div>

答案 1 :(得分:1)

此正则表达式将起作用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="test"/>。此外,我会使用超时,因此您只需在用户停止输入

后检查并替换字符

&#13;
&#13;
   List<int> tempList = new List<int>() { 1, 2, 2, 3, 4, 5 };

   // array == [2, 2]
   List<int> array = tempList
     .GroupBy(x => x)
     .Where(x => x.Count() > 1)
     .SelectMany(x => Enumerable.Repeat(x.Key, x.Count()))
     .ToList();
&#13;
src/test/java
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我认为您需要将代码拆分为2个事件:blurkeyup/keypress

blur的正则表达式验证整个输入

^(?:\d{3}-\d{7}-\d{2}|\d{2}(?:-\d{2}){2}-\d{3}-\d{2})$)

允许用户输入任何数字和“连字数”组的keyup/keypress事件的正则表达式:

^(?!.*-{2})\d+(?:-(?:\d+)?)*$