Added code to fetch the user's location, and send it as a GET/POST variable

php
Aadhavan Srinivasan 2 years ago
parent 70f91c7586
commit ae036686b8

@ -1,16 +1,46 @@
// const submit = document.getElementById("button"); // const submit = document.getElementById("button");
// submit.addEventListener('click', validate); // submit.addEventListener('click', validate);
mycheckbox = document.getElementById("restricted");
mycheckbox.addEventListener('change',checkboxCallback);
function checkboxCallback(event) {
if (event.currentTarget.checked) {
getLocation();
}
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.log("Your browser does not support geolocation.");
}
}
function showPosition(position) {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
document.getElementById("latitude").value = position.coords.latitude;
document.getElementById("longitude").value = position.coords.longitude;
}
function validate() { function validate() {
//e.preventDefault(); //e.preventDefault();
const url = document.getElementById("URL"); const url = document.getElementById("URL");
const glink = document.getElementById("GLink"); let glink = document.getElementById("GLink");
const error = document.getElementById("error");
// if (!url) { // if (!url) {
// /* Flag */ // /* Flag */
// } // }
let valid = true; let valid = true;
const domainExp = new RegExp("http(s)*:\\/\\/[a-zA-Z0-9\\-]+(\\.[a-zA-Z0-9\\-]+)+"); const domainExp = new RegExp("^http(s)*:\\/\\/[a-zA-Z0-9\\-]+(\\.[a-zA-Z0-9\\-]+)+$");
const filepathExp = new RegExp("[a-zA-Z]+"); const filepathExp = new RegExp("^[a-zA-Z]+$");
const glinkExp = new RegExp("^[a-zA-Z]*$");
let glinkStr = glink.value;
let count = 0; let count = 0;
let index = -1; let index = -1;
let domain = ""; let domain = "";
@ -36,8 +66,18 @@ function validate() {
domain = url.value; domain = url.value;
} }
console.log(domain); console.log(domain);
if (domain.match(domainExp)) /** and is available? */{
const error = document.getElementById("error"); if (glinkStr === "") {
var result = window.confirm("You have left the glink field blank. A random one will be generated for you.");
if (result === false) {
return false;
} else {
glinkStr = getRandomGLink();
}
}
if (domain.match(domainExp) && glinkStr.match(glinkExp))/** and is available? */{
if (error.classList.contains("visible")) { if (error.classList.contains("visible")) {
error.classList.remove("visible"); error.classList.remove("visible");
} }
@ -45,30 +85,43 @@ function validate() {
url.classList.remove("invalid"); url.classList.remove("invalid");
} }
url.classList.add("valid"); url.classList.add("valid");
if (glink.classList.contains("invalid")) {
glink.classList.remove("invalid");
}
glink.classList.add("valid");
error.setAttribute('aria-hidden', true); error.setAttribute('aria-hidden', true);
error.setAttribute('aria-invalid', false); error.setAttribute('aria-invalid', false);
console.log("Valid"); console.log("Valid with url= " +url.value+ " glink=" +glink);
return valid; return valid;
/* var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET','result.php',true);
xmlhttp.send();
console.log(xmlhttp.responseText);*/
} else { } else {
/*flag*/ /*flag*/
const error = document.getElementById("error");
error.classList.add("visible"); error.classList.add("visible");
//error.classList.add("hidden"); //error.classList.add("hidden");
if (!domain.match(domainExp)) {
if (url.classList.contains("valid")) { if (url.classList.contains("valid")) {
url.classList.remove("valid"); url.classList.remove("valid");
} }
url.classList.add("invalid"); url.classList.add("invalid");
} else {
if (url.classList.contains("invalid")) {
url.classList.remove("invalid");
}
url.classList.add("valid");
}
if (!glinkStr.match(glinkExp)) {
if (glink.classList.contains("valid")) {
glink.classList.remove("valid");
}
glink.classList.add("invalid");
} else {
if (glink.classList.contains("invalid")) {
glink.classList.remove("invalid");
}
glink.classList.add("valid");
}
error.setAttribute('aria-hidden', false); error.setAttribute('aria-hidden', false);
error.setAttribute('aria-invalid', true); error.setAttribute('aria-invalid', true);
return false; return false;
} }
} }

Loading…
Cancel
Save