[英]regex positive negative decimal numbers, comma separated
I am having trouble getting the regex right for the validation. 我在使正则表达式正确进行验证时遇到麻烦。 Need multiple decimals, positive, negative numbers, and comma separated to be allowed and NOTHING else.
需要多个小数点,正数,负数和逗号分隔以允许使用,否则不进行其他操作。
Should allow: 应该允许:
41.975481,-87.728222, 41.974141,-87.721569, 41.973247,-87.7135 41.975481,-87.728222,41.974141,-87.721569,41.973247,-87.7135
Shouldn't allow: 不应允许:
41.975481,-87.728222, 41.97TEST4141,-87.721569, 41.973247,-87.7135, test 41.975481,-87.728222,41.97TEST4141,-87.721569,41.973247,-87.7135,测试
This is what I am using but its allowing for characters. 这是我正在使用的,但允许使用字符。
(\-?\d+(\.\d+))(,\s*(\-?\d+(\.\d+)))*
Am I missing something? 我想念什么吗?
$('textarea[name="latlong"]').bind('focusout',function(){
$(".polygon .geoerror").text("");
var polygon = $(this).val();
//remove trailing comma and extra space if its there
polygon = polygon.replace(/,\s*$/, "");
var checkpoly = /(\-?\d+(\.\d+))(,\s*(\-?\d+(\.\d+)))*/;
var evenodd = polygon.split(',').length;
if (checkpoly.test(polygon) && evenodd % 2 == 0) {
$(".polygon .geoerror").text("Polygon string is valid." + polygon);
$(".polygon .geoerror").css("color", "green");
} else if (checkpoly.test(polygon) && evenodd % 2) {
$(".polygon .geoerror").text("Mismatch of lat and long values." + polygon);
$(".polygon .geoerror").css("color", "red");
} else {
$(".polygon .geoerror").text("Not valid." + polygon);
$(".polygon .geoerror").css("color", "red");
}
return false;
});
Based on the code if the user inputs - 41.975481,-87.728222, 41.974141,-87.721569, 41.973247,-87.7135, test - then it tests as valid. 根据代码,如果用户输入-41.975481,-87.728222、41.974141,-87.721569、41.973247,-87.7135,则测试-然后测试为有效。 It needs to error instead.
它需要出错。
This should do it: 应该这样做:
/^(?:\-?\d+\.\d+[, ]*)+$/
https://regex101.com/r/nP9lE8/1 https://regex101.com/r/nP9lE8/1
$('textarea[name="latlong"]').bind('focusout',function(){
$(".polygon .geoerror").text("");
var polygon = $(this).val();
//remove extra space if its there
polygon = polygon.replace(/\s*/g, "");
var nums = polygon.split(',');
var len = nums.length;
var is_even = ( len % 2 == 0 );
if (!is_even) {
$(".polygon .geoerror").text("Mismatch of lat and long values." + polygon);
$(".polygon .geoerror").css("color", "red");
return false;
}
var validNums = true;
for(var i = 0; i < len; i++) {
if(isNaN(nums[i])) {
validNums = false;
break;
}
}
if (validNums) {
$(".polygon .geoerror").text("Polygon string is valid." + polygon);
$(".polygon .geoerror").css("color", "green");
} else {
$(".polygon .geoerror").text("Not valid." + polygon);
$(".polygon .geoerror").css("color", "red");
}
return false;
});
The core of your regex is absolutely okay. 正则表达式的核心绝对可以。 The point(s) you are missing were given in the answer of caeth , but without explanation.
您遗漏的要点已在caeth 的答案中给出 ,但未作解释。
Basically, he added ^
at the beginning and $
at the end, forcing the regex to match the whole word, not just parts of it. 基本上,他在开头加上
^
,在结尾加上$
,迫使正则表达式匹配整个单词,而不仅仅是部分单词。 According to the description on RegExr (Reference -> Anchors) ^
根据RegExr的描述(参考->锚)
^
[m]atches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled.
[m]匹配字符串的开头,如果启用了多行标志(m),则匹配行的开头。 This matches a position, not a character.
这匹配一个位置,而不是一个字符。
Similiarly $
(same source) 类似
$
(来源相同)
[m]atches the end of the string, or the end of a line if the multiline flag (m) is enabled.
[m]匹配字符串的末尾,如果启用了多行标志(m),则匹配行的末尾。 This matches a position, not a character.
这匹配一个位置,而不是一个字符。
So a corrected (and a little bit cleaner) version of your regex would be something like 因此,经过修正(更清晰一点)的正则表达式版本将类似于
/^(-?\d+\.\d+)(\s*,\s*-?\d+\.\d+)+$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.