This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP and Javascript problem

 
arpit singla
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP submitting form data to servlet. One of the textfield is ssn. If sb. doesn't fills the numbers in the "ssn" textfield, i want to check that on client side using Javascript. Please let me know how to do it properly. I tried with the following code but it gives me the alert message even before that JSP having the ssn textfield is displayed.
Please let me know the correct format.
<form name="register_form">
<tr>
<td>
SSN:
<input type="text" name="ssn" value="<%=request.getAttribute("ssn")%>" size="9" maxlength="9">
<script>
var testssn = register_form.ssn.value;
if(testssn==null)
alert("Please fill in the ssn");
</script>
</td>
</tr>
</form>
thanks a lot
 
Detlev Beutner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be rude: This is not a JSP/Servlet question, it's just JavaScript...
But to be kind: It seems that first, someone has to input some "ssn"-value in the first (here not cited) page. This page calls the JSP. Then in the JSP the "ssn"-value is taken and put in a form again. Now, in your example, the JavaScript is run anyhow, before the client-side user has the possibility to edit the form again.
So, you can check the input in the first page OR in the second (JSP), by JavaScript, with the help of "onClick" or "onSubmit", for example.
Hope it helps
Detlev
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic