hi

i make textarea for user to insert your comment on topic and press submit to enter comment in database , problem i want to privant user to write empty string and press submit to enter it in database and i want to make this validation on textarea or submit button.

Recommended Answers

All 4 Replies

here you go

<script type='text/javascript'>
function isEmpty(elem, errMsg){
	if(elem.value.length == 0){
		alert(errMsg);
		elem.focus();
		return true;
	}
	return false;
}
</script>
<form>
Name : <input type='text' id='name'/>
<input type='button' 
	onclick="isEmpty(document.getElementById('name'), 'Please Enter a Value')"
	value='Check Field' />
</form>

obviously it will work the same with a textarea but i just ripped it out of some of my own code. hope this helps

<input type='button' 
	onclick="isEmpty(document.getElementById('name'), 'Please Enter a Value')"
	value='Check Field' />

that won't validate on submit, it'll validate, but not submit. if you want to validate, you have to conditionally stop the submit, which is done by returning false within an onsubmit event. the function is right, but the call to it is not making full use of its potential.

keeping your function Fungus1487; try:

[incorrect]

EDIT:

I tell a lie! onsubmit is a form event not a submit button event:

<form action="anactionhandler.cgi" method="GET" onsubmit="return ! isEmpty(document.getElementById('name'), 
'Please Enter a Value')">
Name : <input type='text' id='name'/>
<input type='submit' value='Submit!' />
</form>

sorry, my mistake

Ah, don't worry; we all make them.. I made one myself in that post; see edit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.