Dear All, I am new to stackoverflow i have doubt regarding calling jsp from javascript file. my file contain one html file with javascript(home.html) and one jsp file(login.jsp) In html(home.html) file i have 2 textbox and 2 buttons one for login and another for reset.when i click the login button i should call a js for textbox field validation (ie for if any one of the textbox is empty it shows "text fields should not be empty" alert msg to user)if both the textbox have value then it should call a jsp page(login.jsp).Thanks in advance
A:
<form id="myform" action="login.jsp" method="post">
<input name="u" id="u"> Username<br>
<input name="p" id="p" type="password"> Password
</form>
<script>
document.getElementById('myform').addEventListener('submit', function(e) {
if (!document.getElementById('u').value || !document.getElementById('p').value)
e.preventDefault();
}, false);
</script>
Delan Azabani
2010-09-18 10:25:05
Thanks Delan your answer much useful
Mohan
2010-09-18 10:35:26
No problem. If you think that this works for you, you can accept this answer by clicking the tick next to the answer.
Delan Azabani
2010-09-18 10:37:34
<script language...>var name=... var pass=... if(name==" "||pass==" ") { alert("fields should not be empty"); } else{ var jspcall = "login.jsp?param1=value1 window.location.href = jspcall; }</script> i tried like this
Mohan
2010-09-18 10:38:15
A:
Try using jquery
$.post("login.jsp", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});
JapanPro
2010-09-18 10:25:57
jquery is pretty much similar to JavaScript. window. location solution will not be ajax solution rather it will be http transfer.
JapanPro
2010-09-18 10:58:00
A:
<script language="JavaScript">
function val(){ var name=... var pass=... if(name==" "||pass==" ") { alert("fields should not be empty"); } else{ var jspcall = "login.jsp?param1=value1¶m2=value2"; window.location.href = jspcall; } }
Mohan
2010-09-18 10:33:28
This form will not submit at all if JavaScript is disabled. Contrast this to my answer, which degrades gracefully and will still submit, without validation, if JavaScript is disabled.
Delan Azabani
2010-09-18 10:35:07
Ya Delan from my above code i am facing form submission error. i Will try your code. Thanks a lot
Mohan
2010-09-20 04:16:43