I am doing a small exercise using HTML and Javascript. Basically, I have 2 input fields that I need to validate using 1 function in Javascript. The first input asks for the name and if the name is less than 3 characters, there would be an error printed, else, the error is removed. The second one asks for the locality. Now, to validate this, I must first create an array which contains about 3 locations and then compare the location the user entered to see if it is in the array. If it is not, again, an error is printed. The thing is this; I am getting an error on live preview. The error prints "Cannot GET /Msida,Pieta,Marsa" on screen and "Failed to load resource: the server responded with a status of 404 (Not Found)" on the console. What could the problem be?
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Revision</title>
</head>
<body>
<nav id="menu"></nav>
<a>Full Name: </a><input type="text" name="fName" id="fName" ><br/>
<div id="divName"></div><br/>
<a>Locality: </a><input type="text" name="locality" id="locality" ><br/>
<div id="divLocality"></div><br/>
<script src="js/mainScript.js"></script>
<script src="js/detailsScript.js"></script>
</body>
</html>
JavaScript:
var fName = document.getElementById("fName");
var locality = document.getElementById("locality");
var divName = document.getElementById("divName");
var divLocality = document.getElementById("divLocality");
var location = ["Msida", "Pieta", "Marsa"];
function validation()
{
if(this.name == "fName")
{
if(this.value.length < 3)
{
divName.innerHTML = "Name should have atleast 3 characters";
divName.style.color = "red";
}
else if(this.value.length >= 3)
{
divName.innerHTML = "";
}
}
else if(this.name == "locality")
{
if(location.indexOf(this.value) > -1)
{
divLocality.innerHTML = "Location Exists";
divLocality.style.color = "red";
}
else
{
divLocality.innerHTML = "Location doesn't Exist";
divLocality.style.color = "red";
}
}
}
fName.addEventListener('focusout', validation);
locality.addEventListener('focusout', validation);
Thanks!
JavaScript location redirectsOnly use JavaScript redirects if you can't do server side or meta refresh redirects. While Google attempts to render every URL Googlebot crawled, rendering may fail for various reasons.
To redirect to another page in JavaScript, you can use window. location. href property, window. location will also work and if you want you can use window.
Just rename your location
variable. Your current code is not working because you are basically re-assigning the URL. See window.location.
JavaScript will actually execute var location = ['a', 'b', 'c'];
in 2 parts, declaration and assignment. The declaration part (var location
) will be simply ignored because location
is always declared initially (at least in the browser). And the re-assignment part (location = ....
) will redirect you to location.toString
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With