Check textboxes for empty string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daitasri
    New Member
    • Feb 2008
    • 18

    #1

    Check textboxes for empty string

    Hi
    I m using javascript in HTML. I want to know how to validate the text entered in a textbox (for empty strings). My requirement is that my code should not allow empty text (not even spaces -trailing n leading). I have used some code which is checking only wen v do not enter any text. But its not validating when v enter some number of blank spaces.Please help .
    Thanks
    daitasri
  • vikas1111
    New Member
    • Feb 2008
    • 122

    #2
    Originally posted by daitasri
    Hi
    I m using javascript in HTML. I want to know how to validate the text entered in a textbox (for empty strings). My requirement is that my code should not allow empty text (not even spaces -trailing n leading). I have used some code which is checking only wen v do not enter any text. But its not validating when v enter some number of blank spaces.Please help .
    Thanks
    daitasri

    Please post your code....

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

      #3
      Originally posted by daitasri
      Hi
      I m using javascript in HTML. I want to know how to validate the text entered in a textbox (for empty strings). My requirement is that my code should not allow empty text (not even spaces -trailing n leading). I have used some code which is checking only wen v do not enter any text. But its not validating when v enter some number of blank spaces.Please help .
      Thanks
      daitasri

      Hi, for this you can use String function like indexOf('the string you want to check', and the delimiter like space so and so) - if the delimiter is present it will return the index at which it is present otherwise it will return -1.
      I give u a code for email validation. This may be helpful for you

      Code:
      <html>
      <head>
      <script type="text/javascript">
      function validate_email(field,alerttxt)
      {
      with (field)
      {
      apos=value.indexOf("@");
      dotpos=value.lastIndexOf(".");
      if (apos<1||dotpos-apos<2) 
        {alert(alerttxt);return false;}
      else {return true;}
      }
      }function validate_form(thisform)
      {
      with (thisform)
      {
      if (validate_email(email,"Not a valid e-mail address!")==false)
        {email.focus();return false;}
      }
      }
      </script>
      </head><body>
      <form action="submitpage.html"
      onsubmit="return validate_form(this);"
      method="post">
      Email: <input type="text" name="email" size="30">
      <input type="submit" value="Submit"> 
      </form>
      </body></html>
      If more doubts means pls post your code

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        hi ...

        have a look at the following example, that uses another simple validation method:

        [CODE=javascript]// our value
        var v = ' 1234 ';

        // the validate function
        function validate(val) {
        // first we trim leading and trailing spaces
        val = trim(val);

        // now we check for the length of the string
        // and return true when v is not an 'empty string'
        return val.length != 0;
        }

        // trim function that uses regex to replace the spaces
        function trim(val) {
        var ret = val.replace(/^\s+/, '');
        ret = ret.replace(/\s+$/, '');

        return ret;
        }

        // it will alert true ... so tha validation says ok ...
        alert(validate( v));
        [/CODE]
        kind regards

        Comment

        • daitasri
          New Member
          • Feb 2008
          • 18

          #5
          Hi

          I used the following code ,which trims any number of leading and trailing spaces. I also want to know how to restrict the number of spaces between two words to one.For example :
          It should allow strings like "A bc" but not something like "A bc" or "A bc". Please help me on this.It shud not allow two or more spaces between the two words.
          [CODE=javascript] function trim(str)
          {

          str=str.replace (/^\s*/,"").replace (/\s*$/,"");
          alert('user id after trim'+str);
          return str;

          }[/CODE]

          Thanks in Advance
          daitasri
          Last edited by daitasri; Mar 17 '08, 06:09 AM. Reason: small change

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            The following code will remove the white spaces from the begining and end of the String, if it is useful to you just use it

            [HTML]<Html>
            <head>
            <script type="text/javascript">
            function Trim(str)
            { while(str.charA t(0) == (" ") )
            { str = str.substring(1 );
            }
            while(str.charA t(str.length-1) == " " )
            { str = str.substring(0 ,str.length-1);
            }
            return str;
            }
            var myString = " This is my string ";
            var newString = Trim(myString);
            alert("myString length: " + myString.length );
            alert(myString) ;
            alert("newStrin g length: " + newString.lengt h);
            alert(newString );
            </script>
            </head>
            </html>[/HTML]

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              Originally posted by daitasri
              ... I also want to know how to restrict the number of spaces between two words to one.For example : It should allow strings like "A bc" but not something like "A bc" or "A bc". Please help me on this.It shud not allow two or more spaces between the two words.
              ...
              here is an example:

              [CODE=javascript]var s = 'A bc';

              function trim_inner_spc( s) {
              s = s.replace(/\s+/g, ' ');
              return s;
              }

              alert(trim_inne r_spc(s));
              [/CODE]
              use it after trimming the trailing and leading spaces ...

              kind regards

              Comment

              • daitasri
                New Member
                • Feb 2008
                • 18

                #8
                Originally posted by gits
                here is an example:

                [CODE=javascript]var s = 'A bc';

                function trim_inner_spc( s) {
                s = s.replace(/\s+/g, ' ');
                return s;
                }

                alert(trim_inne r_spc(s));
                [/CODE]
                use it after trimming the trailing and leading spaces ...

                kind regards

                Hi
                Thanks a lot .. Its working perfectly.
                Regards
                daitasri

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  no problem ... post back to the forum anytime you have more questions ...

                  kind regards

                  Comment

                  Working...