Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
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 asp.net form is limited.

any help or examples would be appreciated.

thanks
 
Here is what I use
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;
		e.focus();
	} 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;
		e.focus();
	} 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.

Chris.

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


Code:
function validatetxtandcheck()
{
[indent]var txtObj = document.getElementByID('textbox1');[/indent]
[indent]var chckObj = document.getElementByID('checkbox1');[/indent]

[indent]if(txtObj.value !="" && chckObj.checked == true)[/indent]
[indent]{[/indent]
[indent][indent]alert('Textbox is empty, checkbox is checked');[/indent][/indent]
[indent][indent]return false;[/indent][/indent]
[indent]}[/indent]

}





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

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 :

Feherke.
feherke.ga
 
that's exactly what I need but I am using asp.net 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 (event.target.txt1.value && ! event.target.cbo1.value) {
event.preventDefault();
alert('textbox is not empty but a combox box is')
}
}, false);
}
</script>
 
Hi

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

dvannoy said:
I'm trying to fire the event using the onClientClick event
Sorry, no idea about asp.net and neither about onClientClick. But sounds bad. Validation should be performed on [tt]form[/tt]'s [tt]onsubmit[/tt]. 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 [tt].checked[/tt] test [tt].value[/tt].


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top