User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,021 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,364 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 850 | Replies: 4
Reply
Join Date: Nov 2006
Posts: 8
Reputation: amtallah is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amtallah amtallah is offline Offline
Newbie Poster

Question help in validation by javascript

  #1  
Apr 30th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: help in validation by javascript

  #2  
Apr 30th, 2007
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
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: help in validation by javascript

  #3  
Apr 30th, 2007
Originally Posted by Fungus1487
<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>
Last edited by MattEvans : Apr 30th, 2007 at 6:53 am.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: help in validation by javascript

  #4  
Apr 30th, 2007
sorry, my mistake
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: help in validation by javascript

  #5  
Apr 30th, 2007
Ah, don't worry; we all make them.. I made one myself in that post; see edit.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 10:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC