Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 仅接受数字的输入文本字段-破折号_Jquery_Html - Fatal编程技术网

Jquery 仅接受数字的输入文本字段-破折号

Jquery 仅接受数字的输入文本字段-破折号,jquery,html,Jquery,Html,我正在尝试使用jQuery控制输入字段以前我只有数字,可以使用$(this).autoNumeric(),现在我需要以“数字-破折号”的形式更改输入字段 我尝试了这个网站和其他网站的一些链接,但没有成功, 我只是UI的初学者,请帮助我。此解决方案只允许您输入数字和一个破折号: var dashCount=0; 函数isNumberKey(evt) { var charCode=(evt.which)?evt.which:event.keyCode; if(charCode==45){ dash

我正在尝试使用jQuery控制输入字段以前我只有数字,可以使用
$(this).autoNumeric()
,现在我需要以“数字-破折号”的形式更改输入字段

我尝试了这个网站和其他网站的一些链接,但没有成功,
我只是UI的初学者,请帮助我。

此解决方案只允许您输入数字和一个破折号:


var dashCount=0;
函数isNumberKey(evt)
{
var charCode=(evt.which)?evt.which:event.keyCode;
if(charCode==45){
dashCount++;
}

如果((charCode>47&&charCode),您将需要使用正则表达式。我不会完全依赖HTML5输入掩码

/^\d+(-\d+)*$/
如果没有更强的约束条件,应该足够了

$("#myfield").change(function(){ 
  var isValid = $(this).val().match(/^\d+(-\d+)*$/);
  if(!isValid){
    alert('only numbers and hyphesn please');
    return false;
 }
});
简单html:

<input id="myfield">
这将需要

2
2-3
2.3
2.3-4
2.3.4-5
2.3.4-5.6
2.3.4-5.6.7

演示:

我的意思是3.4.0-54.0,但它也允许其他字符。我正在通过类使用,但它不起作用,像这样做,它允许输入所有字符,我想限制
$('.left_length')。更改(function(){var pattern=/^\d+(\.\d{1,2})?(\.\d{1,2})?(\d+(\.\d{1,2})(\.\d{1,2})?$/;var isValid=$(this.val().match(模式);if(!isValid){alert('请仅限数字或由连字符分隔的有效范围');返回false;})
谢谢briansol…你的解决方案部分解决了我的问题…比如允许进入,但我做了一些变通并解决了它。
 $("#myfield").change(function(){ 
    var pattern = /^\d+(\.\d{1,2})?(\.\d{1,2})?(-\d+(\.\d{1,2})?(\.\d{1,2})?)?$/;
    var isValid = $(this).val().match(pattern);
    if(!isValid){
       alert('only numbers or valid range separated by hyphens please');
     return false;
   }
});
2
2-3
2.3
2.3-4
2.3.4-5
2.3.4-5.6
2.3.4-5.6.7