Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.




I need to display a message if a textbox is not empty but a combox box is.

all examples I have found check if a single textbox is empty.

so if txt1 has txt but cbo1 does not, I would like a message displayed. I'm trying to do this using java script because my room on my form is limited.

any help or examples would be appreciated.


RE: Validate

Here is what I use

CODE --> javascript

function isFilled(e) {
// Test for a text field being an empty string. 
	if (e.value === "") {
		alert('Please enter a value for '+e.rel+'!');
		return false;
	} else {
		return true;

function isChecked(e) {
// Test for a checkbox not being set 
	if (e.checked != true) {
		alert('Checkbox '+e.rel+' is required!');
		return false;
	} else {
		return true;

Use the rel attribute of the elements to hold a message to tell the user what and why, and trigger the test using the onBlur event.


Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.

RE: Validate

So what's the trouble? You should be able to extrapolate the code needed to check both things from the code for checking for one of them at a time.

Checking is pretty much the same for both items.

You just need to decide when you are going to check for it.

Assuming your checkbox has an ID of "textbox1" and your checkbox has an ID of "checkbox1" then:


function validatetxtandcheck()
var txtObj = document.getElementByID('textbox1');
var chckObj = document.getElementByID('checkbox1');
if(txtObj.value !="" && chckObj.checked == true)
alert('Textbox is empty, checkbox is checked');
return false;

Phil AKA Vacunita
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: Validate

Thanks guys...

I will try out the code and make adjustments

so, I have a textbox and a combo box

if the textbox has text and the combobox does NOT have a value selected that's when I want the alert to show.

RE: Validate


Please note that providing a simplified working example of the form you try to enhance may help you get better answers.

For example I thing the guy misunderstood you as their JavaScript codes are using checkbox and radio button specific checks, while I have the feeling you have a dropdown. Though I may be wrong.

Here is a working example of how I would do it strictly based on what you specified :


RE: Validate

that's exactly what I need but I am using controls not html controls. sorry, I'm not a java expert. I'm trying to fire the event using the onClientClick event

<script type="text/javascript">
function validate() {
document.forms[0].addEventListener('b1', function (event) {
if ( && ! {
alert('textbox is not empty but a combox box is')
}, false);

RE: Validate


Quote (dvannoy)

I am using controls not html controls
JavaScript runs in the browser. In the browser there is no anymore, just what it wrote to the standard output. And that should be HTML.

Quote (dvannoy)

I'm trying to fire the event using the onClientClick event
Sorry, no idea about and neither about onClientClick. But sounds bad. Validation should be performed on form's onsubmit. As you can see from the fiddle's setting, my code is intended to be executed onLoad -- then it attaches the event listener which will perform the validation.

If you still want to bind it to you a button's click, better go with Phil's code, just instead of .checked test .value.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close