java - Calling javascript validation function before submiting and calling servlet -
java - Calling javascript validation function before submiting and calling servlet -
i trying validate form , after calling servlet , fails because form doesn't validete. i' ve pasted code below. give me tips , help issue?
class="snippet-code-js lang-js prettyprint-override">function validateform() { if(document.frm.username.value=="") { alert("user name can not left blank"); document.frm.username.focus(); homecoming false; } else if(document.frm.pwd.value=="") { alert("password can not left blank"); document.frm.password.focus(); homecoming false; } }
class="snippet-code-html lang-html prettyprint-override"><%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <link href="/loginexample/css/style.css" rel="stylesheet" type="text/css" /> <script language="javascript" type="text/javascript" src="/loginexample/js/validation.js"></script> <title>login example</title> </head> <body> <form name="loginform" action="login" method="post" onsubmit="return validateform();"> <p> come in user name: <input type="text" name="username"><br> come in password: <input name="password" type="password"><br> <input type="submit"> </p> </form> <a href="register.jsp"><input type="button" value="register" name="sign in"></a> </body> </html>
change javascript function follows,
function validateform(event) { event.preventdefault(); // prevent submit event. if(document.loginform.username.value=="") { alert("user name can not left blank"); document.loginform.username.focus(); homecoming false; } else if(document.loginform.password.value=="") { alert("password can not left blank"); document.loginform.password.focus(); homecoming false; } else { document.loginform.submit();// fire submit event } }
also, pass submit event in function phone call this,
<form name="loginform" action="login" method="post" onsubmit="return validateform(event);">
the approach provided crowder good, might have handle submit event beingness fired anyway if validation fails.
demo fiddle
java javascript servlets
Comments
Post a Comment