tags:

views:

109

answers:

3

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
Thanks Delan your answer much useful
Mohan
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
<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
A: 

Try using jquery

$.post("login.jsp", { name: "John", time: "2pm" },
   function(data){
     alert("Data Loaded: " + data);
});

ref: http://api.jquery.com/jQuery.post/

JapanPro
Thanks JapanPro but i dont know about jquery.can we use window.location
Mohan
thanks JapanPro that link is quite useful to me learn about jquery
Mohan
jquery is pretty much similar to JavaScript. window. location solution will not be ajax solution rather it will be http transfer.
JapanPro
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&param2=value2"; window.location.href = jspcall; } }

Mohan
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
Ya Delan from my above code i am facing form submission error. i Will try your code. Thanks a lot
Mohan