This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to validate both check box and textbox field at a time

 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i created jsp page..in it i used check boxes and textboxes..after clicking checkbox it is not validating the text...i need to validate both the fields..here is my code..its urgent for me...i need help where the code is wrong..

login.jsp



and javascript code is



Thanks and waiting for the reply,
madhu.


 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change this line of the code. All your checkboxes and text fields have the same name. What you would want to do is check if a checkbox is checked, and if it is, check if the corresponding text field has a value. You can do it like this
You can implement this similar logic for the other text field too...
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Thanks for your reply..i tried it is working...the problem is if i didnt check the check-box it is not showing any alert message that checkbox should be selected...can you help me out regarding that..i need both check box and textfield should be validated...that means if i check checkbox and left text field it should ask for 'Enter the value inText box'..vice-versa..or suggest me where to change the code...

Thanks and waiting for the reply,
madhu.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
madhuri both your checkboxes and text fields have the same name. So document.getElementsByName('balrdys') will return both of them. Then when you iterate over the elements, you'll get an error when the element is a text box as there is no checked property on text boxes. You can assign an ID to all the checkboxes and check if any of them are checked using getElementById as I already gave you an example...
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i got your point..if i didnt check the checkbox it is directly loading..that means it is not showing the errors...i had pasted the same example what you had given to me...i need help that if i left any one and press submit..it is should throw an error..i changed the code as follows..



Thanks,madhu.

 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
madhuri getElementById always returns only one element. You can't iterate over what it returns. And IDs should be unique in a page. What I want to say is, the way the code I gave checks if a checkbox is checked and if it is, its corresponding text box has a value, you can introduce two other conditions in your javascript. Just like you are doing in your code right now, if any of the checkbox is checked, you set the value of a variable to see if a checkbox was checked. So it will go something like this

So what you do is you give a separate ID to your checkboxes and text boxes and then check them using getElementById. This is just one way of implementing it, there are other ways of implementing the same thing as well...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic