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
Shouldn't allow:
41.975481,-87.728222, 41.97TEST4141,-87.721569, 41.973247,-87.7135, test
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. It needs to error instead.
$('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.
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) ^
[m]atches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled. 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. 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+)+$/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.