I have a form validaion script that is called by:
<form id="quote" name="quote" method="get" onsubmit="return formvalidation();" action="#">
The HTML input:
<input id="sitesinput" name="sitesinput" maxlength="3" value="0" onChange='countAndRun()' />
This is part of the function, and works just fine, doesn’t submit the form if the input is blank, zero etc:
var x=document.getElementsByName("sitesinput")[0].value;
if (x==null || x=="" || x==0)
{
alert("You have not selected a total amount of sites.")
return false;
}
however if I add a couple of lines, to focus the window on the input and give it a red border, the form submits regardless of what is inputted:
document.getElementByName("sitesinput").style.border="1px solid #F00"
document.getElementByName("sitesinput").focus()
add into the function like this:
var x=document.getElementsByName("sitesinput")[0].value;
if (x==null || x=="" || x==0)
{
alert("You have not selected a total amount of sites.")
document.getElementByName("sitesinput").style.border="1px solid #F00"
document.getElementByName("sitesinput").focus()
return false;
}
Thanks for looking.
B.
My guess would be that the Javascript is failing somewhere. If there are JS errors because, for example,
.styleisundefined, your form will end up posting.You’ll also note that there’s no
getElementByName()method in Javascript but there is agetElementsByName()method (Elements, plural). This will be likely what’s causing your problem.