正则表达式,大于,小于,之间

时间:2013-12-05 00:44:20

标签: javascript jquery regex validation

我需要为这些情况创建客户端验证

Syntax      Result filter                                     Example
X+          Return all items with X or more                   4+
X-          Return all items with X or less                   10-
X-Y         Return all items between (inclusive) X and Y      4-8

可能最好的方法是做一些链接这个

function allnumericplusminus(inputtxt) {
var regex = /^[-+]?[0-9]+$/;
if (inputtxt.match(regex)) {
    alert('Correct...Try another');
    return true;
} else {
    alert('Please input correct format');
    return false;
}
}

主要问题是因为我不知道如何为这种特定情况编写正则表达式。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

var regex = /^\-?\d+[\+\-](\-?\d+)?$/;

更新: 正则表达式将传递像“4 + 8”这样的表达式但不需要它们,所以正则表达式应该是这样的:

var regex = /^\-?\d+([\+\-]|(\-\-?\d+)?)$/;

答案 1 :(得分:0)

尝试:

function checkIt(text){
  return text.toString().match(/^\d+(+|-)?\d?$/);
}