Javascript validation for spaces in text box

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

    Javascript validation for spaces in text box

    My Text box can accept all types of data like alphabets,numer ic,special characters ... Should give an error message 'if space is not a valid value '
    when i am clicking a button... in
    asp.net with c# through javascript.(jav ascript validation after clicking a button).
    Last edited by kenobewan; Mar 24 '08, 11:48 AM. Reason: Changed title to something meaningful
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Originally posted by kiran83
    My Text box can accept all types of data like alphabets,numer ic,special characters ... Should give an error message 'if space is not a valid value '
    when i am clicking a button... in
    asp.net with c# through javascript.(jav ascript validation after clicking a button).
    This is still a JS problem. Suggest using click event and regular expressions. HTH.

    Comment

    • kiran83
      New Member
      • Feb 2008
      • 18

      #3
      please send me javascript code for that(Space validation) it is very importent in my project...

      Comment

      • kiran83
        New Member
        • Feb 2008
        • 18

        #4
        i was tryed the below code


        function validate()
        {
        if (document.getEl ementById("<%=T extBox1.ClientI D%>").value=="" )
        {
        alert("No Feild can not be blank");
        document.getEle mentById("<%=Te xtBox1.ClientID %>").focus();
        return false;
        }
        }

        this code only validate without blank but i want to without spaces validation

        please help me...

        Comment

        • kiran83
          New Member
          • Feb 2008
          • 18

          #5
          please reply me for the abobe question...

          Comment

          • malav123
            New Member
            • Feb 2008
            • 217

            #6
            Originally posted by kiran83
            please reply me for the abobe question...
            Hi Kiran,

            You can use Ascii value for validation... or you can use trim function to remove all spaces from your text boxes...

            Comment

            • kiran83
              New Member
              • Feb 2008
              • 18

              #7
              how to use ascii value or trim for that text box

              Comment

              • malav123
                New Member
                • Feb 2008
                • 217

                #8
                Originally posted by kiran83
                how to use ascii value or trim for that text box
                HI,
                Fisrt of all tell me what you needs exactly ? do you want to put validation in which spaces not allows in textbox or you want to cheks only blank textbox validation ?? I think you don't want users to put spaces in text box.. right ? tell exact requirement than i will tell you how to use it because i have already used such validation in my project....

                Comment

                • kiran83
                  New Member
                  • Feb 2008
                  • 18

                  #9
                  validation in which spaces not allows in textbox

                  Comment

                  • kiran83
                    New Member
                    • Feb 2008
                    • 18

                    #10
                    please reply me for the above question...

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #11
                      have a look at the following example:

                      [CODE=javascript]function validate() {
                      var field = document.getEle mentById("<%=Te xtBox1.ClientID %>");
                      var val = field.value.rep lace(/^\s+|\s+$/, '');

                      if (val.length == 0) {
                      alert('wrong value');
                      }
                      }
                      [/CODE]
                      kind regards

                      Comment

                      • kiran83
                        New Member
                        • Feb 2008
                        • 18

                        #12
                        Thank you for sending the code.


                        kiran

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5388

                          #13
                          no problem ... does it work for you?

                          kind regards

                          Comment

                          • malav123
                            New Member
                            • Feb 2008
                            • 217

                            #14
                            If you want to validate the textboxes for space than you can use followin java script function,

                            function trimAll(sString )
                            {
                            while (sString.substr ing(0,1) == ' ')
                            {
                            sString = sString.substri ng(1, sString.length) ;
                            }
                            while (sString.substr ing(sString.len gth-1, sString.length) == ' ')
                            {
                            sString = sString.substri ng(0,sString.le ngth-1);
                            }
                            return sString;
                            }

                            Comment

                            • gits
                              Recognized Expert Moderator Expert
                              • May 2007
                              • 5388

                              #15
                              yes ... that is another way to trim a value that is done with the replace-method and a regex in the above code:

                              [CODE=javascript]var val = field.value.rep lace(/^\s+|\s+$/, '');
                              [/CODE]
                              it could be just one line ;)

                              kind regards

                              Comment

                              Working...