I need to change code below to use "onsubmit" instead of "onblur" where javascript will validate if my latitude/longitude format are correct. The format of lat/long is (numbers.numbers, numbers.numbers) where numbers=any length and between 0-9. All help are appreciated.
我需要更改下面的代码以使用“onsubmit”而不是“onblur”,其中javascript将验证我的纬度/经度格式是否正确。 lat / long的格式是(numbers.numbers,numbers.numbers),其中number =任意长度且在0-9之间。所有帮助表示赞赏。
HTML
Latitude, Longitude <input type="text" id="formatGPS" onblur="checkGPS(this);" /><button>Enter</button>
Javascript
function checkGPS(inputGPS) {
var regex = new RegExp("^[0-9].[0-9], [0-9].[0-9]$", "i");
var formatGPS = inputGPS.value;
if ((formatGPS.length > 0) && !regex.test(formatGPS)) {
alert("The format is always as follows: numbers.numbers, numbers.numbers (With numbers=any length)");
}
}
http://jsfiddle.net/Qc2bd/104/
1 个解决方案
#1
Try this regexp:
试试这个正则表达式:
var regex = new RegExp("^[0-9]+\.[0-9]+, [0-9]+\.[0-9]+$", "i");
Although this will force a number to be always there, so zero would be 0.0. Also 000.000 would be valid. If this is what you want, this will work :).
虽然这会强制数字始终存在,但零将为0.0。 000.000也是有效的。如果这是你想要的,这将工作:)。
Another thing is whether you need or not negative values, as sideroxylon pointed out, in that case the regexp would be:
另一件事是你是否需要负值,正如sideroxylon指出的那样,在这种情况下,正则表达式将是:
^-?[0-9]+\.[0-9]+, -?[0-9]+\.[0-9]+$
#1
Try this regexp:
试试这个正则表达式:
var regex = new RegExp("^[0-9]+\.[0-9]+, [0-9]+\.[0-9]+$", "i");
Although this will force a number to be always there, so zero would be 0.0. Also 000.000 would be valid. If this is what you want, this will work :).
虽然这会强制数字始终存在,但零将为0.0。 000.000也是有效的。如果这是你想要的,这将工作:)。
Another thing is whether you need or not negative values, as sideroxylon pointed out, in that case the regexp would be:
另一件事是你是否需要负值,正如sideroxylon指出的那样,在这种情况下,正则表达式将是:
^-?[0-9]+\.[0-9]+, -?[0-9]+\.[0-9]+$