rozerro rozerro - 3 months ago 6
HTML Question

Can not validate form with Struts

I can not validate a form with this code? A popup does not appear.

<script type="text/javascript">
function validate_login_form() {
if (document.login_form.email.value == "" || document.login_form.password.value == "") {
alert('Some fields are empty');
return false;
}
return true;
}
</script>


<form id="login_form" action="login" method="post" onsubmit="return validate_login_form()">

<s:textfield name="email" key="label.email" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />

<footer class="clearfix">
<p><span class="info">?</span><a href="recover.jsp">Forgot password?</a> <br /> <a class="reg" href="register.jsp">Register</a></p>
</footer>
</form>


What is the reason?

It the content of function will be

alert('Some fields are empty');
return false;


then alert appers then something is with selectors?

Answer

Add name attribute to form tag

<form name="login_form"

The document element can reference a form by name like this

document.login_form or use document.forms[0] if you have only one form.

Comments