ABJ ABJ - 1 year ago 94
Javascript Question

Alert box redirects to another page

I have a form and a form validation code. For third form when text fields are empty and submit button is clicked an alert box is displayed which prompt about missing fields which is good. But when I click ok on that alert box it disappears and current page will redirect on another page(which is not good). It have to stay on that page after alert box disappears. Only this is happening with third form not with first two forms. Here is the code:


<form name="myForm3" action="kbc.jsp" method="post" onsubmit="validate_kbc()">
<br/>MSISDN:<input type="text" name="MISDN" maxlength="11">
<br/>SR:<input type="text" name="SR" maxlength="14">
<br/><input type="submit" value="Submit">
<br/><input type="reset" value="Reset">


function validate_kbc()
var k = document.forms["myForm3"]["MISDN"].value;
var l = document.forms["myForm3"]["SR"].value;
if(k == "" || l == "")
alert("Manadotry fields should not left blank");
// document.myForm1.MISDN.focus();
// document.myForm1.SR.focus();

return false;
else if(!k.match(numbers))
alert('Please input numeric characters only in MSISDN field');
return 0;
else if(!l.match(numbers))
alert('Please input numeric characters only in SR field');
return false;
return true;


Connection dbConnection =(Connection)DriverManager.getConnection( "jdbc:mysql://" ,"root" , "123");
Statement st = dbConnection.createStatement();
ResultSet rs = null;
String misdn=request.getParameter("MISDN");
String sr=request.getParameter("SR");
int i=st.executeUpdate("INSERT INTO kbc(MSISDN,SR) VALUES('"+misdn+"','"+sr+"')");


Answer Source

Try onsubmit="return validate_kbc()" and remove else = return true; from the function. The input is either false and the function will give a warning or there is no false input and the input of the form will be prosessed by the kbc.jsp

I hope this will work otherwise look at: http://www.w3schools.com/js/js_form_validation.asp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download