将正则表达式添加到js代码中

时间:2017-07-04 09:13:04

标签: javascript jquery html

如何将pattern =“[0-9] +”添加到此js代码中的div?

var max_fields      = 10; //maximum input boxes allowed
var wrapper         = $(".input_fields_wrap"); //Fields wrapper
var add_button      = $(".add_field_button"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
    x++; //text box increment
    $(wrapper).append('<div class="delivery-num-input"><input type="text"  pattern="[0-9]+" class="form-control delivery_number_plus"/><a href="#" class="remove_field btn btn-sm btn-danger">Remove</a></div>'); //add input box
}
});

1 个答案:

答案 0 :(得分:0)

据说here。您可以通过编程方式将模式添加到输入中,如下所示:

var regex = /\d+/;
var element = wrapper.find('input');
element.setAttribute("pattern", regex.source);