Skip to content

Commit ce7e10f

Browse files
authored
Implement user location validation on form submission
1 parent c15fe89 commit ce7e10f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function onSubmit() {
2+
var ga = new GlideAjax('UserLocationUtils');
3+
ga.addParam('sysparm_name', 'getUserLocationCoords');
4+
ga.getXMLAnswer(function(response) {
5+
var locData = JSON.parse(response);
6+
if (!locData) {
7+
g_form.addErrorMessage("No assigned location found for your profile.");
8+
return false;
9+
}
10+
11+
navigator.geolocation.getCurrentPosition(function(position) {
12+
var userLat = position.coords.latitude;
13+
var userLng = position.coords.longitude;
14+
var allowedLat = locData.latitude;
15+
var allowedLng = locData.longitude;
16+
var locName = locData.name;
17+
18+
var R = 6371;
19+
var dLat = (userLat - allowedLat) * Math.PI / 180;
20+
var dLng = (userLng - allowedLng) * Math.PI / 180;
21+
var a = Math.sin(dLat / 2) ** 2 +
22+
Math.cos(allowedLat * Math.PI / 180) *
23+
Math.cos(userLat * Math.PI / 180) *
24+
Math.sin(dLng / 2) ** 2;
25+
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
26+
var distance = R * c;
27+
28+
if (distance > 10) { // 10 km tolerance
29+
alert("You are " + distance.toFixed(2) + " km away from your registered office: " + locName);
30+
g_form.addErrorMessage("Location validation failed.");
31+
return false;
32+
} else {
33+
g_form.addInfoMessage("Location validated successfully within range of " + locName);
34+
return true;
35+
}
36+
});
37+
});
38+
return false; // Wait for async location validation
39+
}

0 commit comments

Comments
 (0)