|
-
Jun 25th, 2005, 12:54 PM
#1
Thread Starter
Addicted Member
Javascript check if empty problem
Hey,
I have a textfield and a textarea. Now, i want to check if either of the textboxes are empty when the user submits the form. I've already added a script that checks the boxes when the user 'clicks' the submit button and if one of the textboxes is empty will display an error. BUT... if the user has entered text in the first box (the textfield) and presses enter without giving the second box (the textarea) focus, the form will submit successfully :-S Even though the textarea is empty.
Anyone help me in solving this problem Just ask if you need more explanation :-D
Thanks, BIOSTALL
-
Jun 25th, 2005, 01:04 PM
#2
Member
Re: Javascript check if empty problem
-
Jun 25th, 2005, 01:28 PM
#3
Fanatic Member
Re: Javascript check if empty problem
try this:
HTML Code:
<html>
<head>
<title>Title</title>
</head>
<body>
<form name="form" method="POST" action="script">
<input type="text" name="T1" size="20">
<textarea rows="2" name="S1" cols="20"></textarea><input type="button" onclick="JavaScript: if(document.form.T1.value == '' || document.form.S1.value == ''){ alert('Please fill the form out'); }else{ window.location='http://www.vbforums.com'; }" value="Button" name="B1">
</form>
</body>
</html>
-
Jun 25th, 2005, 01:44 PM
#4
Thread Starter
Addicted Member
Re: Javascript check if empty problem
I'm using this:
Code:
function checkempty(){
var abc, bcd
abc = document.getElementById('threadtitle');
bcd = document.getElementById('threadmessage');
if (abc.value == ""||bcd.value == "") {
alert("Not all fields contain data");
return false;
}else{
return true;
}}
Then i return the function on the 'onclick' event of the form button.
BIOSTALL
-
Jun 25th, 2005, 01:47 PM
#5
Fanatic Member
Re: Javascript check if empty problem
what do you need added to your code?
-
Jun 25th, 2005, 01:51 PM
#6
Thread Starter
Addicted Member
Re: Javascript check if empty problem
Ahhh.. sorted it. i added the scripts return to the 'onsubmit' part of the form itself. Cheers for your help.
BIOSTALL
-
Jun 27th, 2005, 03:05 AM
#7
Re: Javascript check if empty problem
Is the textbox considered not to be empty if it contains a space?
-
Jun 27th, 2005, 05:19 AM
#8
Thread Starter
Addicted Member
Re: Javascript check if empty problem
If the textbox contains a space then it is considered not empty. In order to check for a space you'd have use something like the 'indexof' function.
BIOSTALL
-
Jan 30th, 2008, 01:29 PM
#9
Junior Member
Re: Javascript check if empty problem
i cant get this code 2 work.
Code:
function checkempty(){
var abc, bcd
abc = document.getElementById('threadtitle');
bcd = document.getElementById('threadmessage');
if (abc.value == ""||bcd.value == "") {
alert("Not all fields contain data");
return false;
}else{
return true;
}}
and i have also called the function in the "onsubmit" of the form.
it just doesnt want to work, please help me, thanks
dont believe in all that you've been told!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
-
Jan 30th, 2008, 03:14 PM
#10
Re: Javascript check if empty problem
-
Feb 4th, 2008, 04:45 AM
#11
Re: Javascript check if empty problem
 Originally Posted by will1233
i cant get this code 2 work.
Are your IDs correct
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|