Check for blank space as first character and trimming textbox i/p

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Divyanshi
    New Member
    • Sep 2010
    • 3

    Check for blank space as first character and trimming textbox i/p

    Hello all,

    I have a textbox, in which i have to perform the following validations

    1)first character cannot be blank space
    2)characters should not exceed 100
    3)if exceeded 100, it should trim the i/p to 100 characters

    How can i do it?
    Please give your valuable suggestions

    Thank you all
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Hi Divyanshi,
    get the textbox content in a variable and check with the two properties as shown below

    Ex:
    Code:
       var txt = document.getElementById('textBox').value;
       if(txt.charAt(0)==' '){
         //what should be done for space
       }
       if(txt.length>100){
         //what should be done when the text content is more than 100 char
       }
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      regarding 2), limit the textbox to 100 chars using the maxlength attribute

      Comment

      • Divyanshi
        New Member
        • Sep 2010
        • 3

        #4
        I tried the following

        Code:
        function validateDetails()
        {
          var TDetails=document.getElementById('<%=txtGDetails.ClientID %>').value;
          if(TDetails=="")
          {
          alert("Please Enter Details");
          document.getElementById('<#=txtGDetails.ClientID').focus();
          return false;
          }
          var count = document.getElementById ('<%=txtGDetails.clientID %>').value.length;
          var txt = document.getElementById('<%=txtGDetails.ClientID %>').value;
          if (count >= 250)
          {
            alert("Details should not exceed 250 characters");
            document.getElementById('<#=txtGDetails.ClientID').focus();
            return false ;
          }
          [B] if (txt.charAt(0)=='')
           {
           alert("First space cannot be blank");
           return false ;
          }[/B]    return true ;
           }
        It seems not working, still if i give space as first character, it is not giving the message

        And also if the character is more than 250, it has to delete the remaining text

        Please help to get this done

        Thanks
        Last edited by Dormilich; Sep 24 '10, 08:27 PM. Reason: please use [code] [/code] tags when posting code

        Comment

        • RamananKalirajan
          Contributor
          • Mar 2008
          • 608

          #5
          Hi,
          For your first problem pls find a working sample below
          Code:
          <html>
          <head>
          <script type="text/javascript">
          function checkSpace(){
            var val = document.getElementById('myText').value;
            if(val.charAt(0)==" "){
              alert("I am found")
            }
          }
          </script>
          </head>
          <body>
           <input type="text" name="myText" id="myText" value=" " />
          <input type="button" value="Check Space" onclick="checkSpace();"/>
          </body>
          </html>
          For your second problem use the substring()

          Thanks and Regards
          Ramanan Kalirajan

          Comment

          Working...