• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic