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
Re: Javascript check if empty problem
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>
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
Re: Javascript check if empty problem
what do you need added to your code?
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
Re: Javascript check if empty problem
Is the textbox considered not to be empty if it contains a space?
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
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
Re: Javascript check if empty problem
Re: Javascript check if empty problem
Quote:
Originally Posted by will1233
i cant get this code 2 work.
Are your IDs correct