regexp: How to check if only spaces were entered in a form

Discussion in 'JavaScript' started by enchance, May 10, 2009.

  1. #1
    Before sending a form, how can I check if only spaces were entered (ragardless of how many there are)? That way I can prevent the form from sending. Any other character is ok as long as it's not just all spaces.
     
    enchance, May 10, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    im not sure of with regex but this function should work:
    
    function isAllSpaces(strTxt) {
        for(i=0; i<strTxt.length; i++)
            if(strTxt.substr(i,1)!=" ") return false;
        return true;
    }
    
    Code (markup):
     
    camjohnson95, May 10, 2009 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi,

    You can test input like:
    
    /^\s+$/.test(input_string)
    
    Code (markup):
    Regards

    Edit: And this will test both for empty strings and spaces only strings
    
    /^\s*$/.test(input_string)
    
    Code (markup):
     
    koko5, May 11, 2009 IP