How do you check if fields are empty?

Discussion in 'JavaScript' started by Imozeb, Mar 4, 2010.

  1. #1
    Okay, I know how to check if fields are null and stuff but how do I check to see if fields have no characters in them for example if I do this.

    Javascript Code:
    
    if(var === null || var === "" || var.length === 0)
    {
       return false
    }
    else
    {
    //code here
    }
    
    Code (markup):
    This only checks for certain types of emptyness. How do I check to make sure that the user inputed at least one character other than the 'space key'?
    Thanks.

    ~imzoeb :)
     
    Imozeb, Mar 4, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try this code:

    
    if(var === null || var.replace(/[\s]/g,"").length===0)
    {
       return false
    }
    else
    {
    //code here
    }
    
    Code (markup):
     
    s_ruben, Mar 5, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    S_ruben, your code didn't work. Plz help!
     
    Imozeb, Mar 5, 2010 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    Can you write here full script? It will be easier to help you.
     
    s_ruben, Mar 5, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay. Here is the script that doesn't work.

    Javascript Code:
    //get text
    inputtext = "";
    inputtext = "text=" + document.getElementById('inputbox').value;
    //validate
    if(inputtext === null || inputtext.replace(/[\s]/g,"").length===0)
    {
    }
    else
    {
    //send text
    	param = inputtext;
    	xmlhttpsend.open("POST../../inputfunction.php",true);
    	xmlhttpsend.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xmlhttpsend.setRequestHeader("Content-length", param.length);
    	xmlhttpsend.setRequestHeader("Connection", "close");
    	xmlhttpsend.send(param);
    }
    Code (markup):
    Thanks.

    ~imozeb
     
    Imozeb, Mar 6, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    I have 2 questions

    1. What doesn't work?
    2. How can inputtext has no char if "inputtext = "text=" + document.getElementById('inputbox').value;"?
     
    s_ruben, Mar 6, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    1. If the user just imputs spaces into the text field instead of letters, numbers, and symbols it still sends the value. (This results in the output box having a line of blank text. I want to only send the text if the user imputs letter or numbers or symbols with or without spaces.)

    2. inputtext can have no chars because the user can (if they are evil) imput just spaces instead of letters, numbers, and symbols, which would result in document.getElementById('inputbox').value = " " (a few spaces).

    Thanks.
     
    Imozeb, Mar 6, 2010 IP
  8. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #8
    Do it by this method:

    
    //get text
    inputboxvalue = document.getElementById('inputbox').value;
    
    //validate
    if(inputboxvalue === null || inputboxvalue.replace(/[\s]/g,"").length===0)
    {
    }
    else
    {
    //send text
        inputtext = "text=" + inputboxvalue;
    	param = inputtext;
    	xmlhttpsend.open("POST../../inputfunction.php",true);
    	xmlhttpsend.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xmlhttpsend.setRequestHeader("Content-length", param.length);
    	xmlhttpsend.setRequestHeader("Connection", "close");
    	xmlhttpsend.send(param);
    }
    
    Code (markup):
     
    s_ruben, Mar 6, 2010 IP
  9. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks s_ruben, it worked! :)
     
    Imozeb, Mar 6, 2010 IP