aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes how to validate both check box and textbox field at a time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how to validate both check box and textbox field at a time" Watch "how to validate both check box and textbox field at a time" New topic
Author

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

madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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

Joined: Mar 30, 2010
Posts: 350
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to validate both check box and textbox field at a time