Validating spaces in a textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uicouic
    New Member
    • Jan 2009
    • 24

    Validating spaces in a textbox

    Hi all. Need help with validating spaces in a textbox using JavaScript. The textbox cannot contain just spaces only.

    If not, upon clicking a save button it will display an alert: "Spaces only not allowed."

    Please advise, thanks. Any help greatly appreciated.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you can use a regular expression for that.
    Code:
    /^\s+$/
    will test for "spaces only" (well, of course you have to apply the appropriate functions too)

    Comment

    • uicouic
      New Member
      • Jan 2009
      • 24

      #3
      Sorry, but is it something like this?

      Code:
      function validateSpaces() {
          if (myStr == myStr.replace(/^\s+$/) {
      strMessage += "<li>Spaces only not allowed.<br>";
      blnAllOkay = false;
      }
      
          if (blnAllOkay != true) {
          window.event.returnValue=false;
          lblMessage.innerHTML = strMessage;
          }
      }
      }
      Last edited by Dormilich; Jan 16 '09, 06:51 AM. Reason: fixed [code] tags

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        probably RegEx.test() rather than String.replace( ).

        Comment

        • uicouic
          New Member
          • Jan 2009
          • 24

          #5
          Its working now. Thanks alot Dormilich!

          Cheers! :)

          Comment

          Working...