Results 1 to 11 of 11

Thread: Javascript check if empty problem

  1. #1

    Thread Starter
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    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

  2. #2
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: Javascript check if empty problem

    Can you post the code?

  3. #3
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    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>
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  4. #4

    Thread Starter
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    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

  5. #5
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: Javascript check if empty problem

    what do you need added to your code?
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  6. #6

    Thread Starter
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    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

  7. #7
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Javascript check if empty problem

    Is the textbox considered not to be empty if it contains a space?

  8. #8

    Thread Starter
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    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

  9. #9
    Junior Member
    Join Date
    Jan 2007
    Location
    england
    Posts
    27

    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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  10. #10
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015

    Re: Javascript check if empty problem

    what is happening now?

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Javascript check if empty problem

    Quote 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
  •  



Click Here to Expand Forum to Full Width