text box validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolodede
    New Member
    • Apr 2009
    • 63

    text box validation

    hi
    thank you very much for replying quickly if i could do anything tell me
    im new to javascript so i have problem with validating text box to accept only positive numbers and not more than 1000

    thanks
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You could use a regular expression:
    Code:
    /^\d+$/
    to test that there are only integers. Once you have that established, use parseInt to convert the string into an integer and test that it's less than 1000:
    Code:
    if (parseInt(str) > 1000) {
       alert("Error ...");
    }

    Comment

    • lolodede
      New Member
      • Apr 2009
      • 63

      #3
      sorry but i dont know regular expression and i try everything didnt work

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        See this and this to get acquainted. If you don't want to use a regular expression, you could create isDigit()/isInteger()/isPositiveInteg er() functions, e.g.
        Code:
        function isDigit(num) {
            if (num.length > 1) return false;
            var str="1234567890";
            return (str.indexOf(num) != -1);
        }

        Comment

        • lolodede
          New Member
          • Apr 2009
          • 63

          #5
          its not working i dont why can use isnan function if yes how

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            isNaN wouldn't be enough. The code I posted for isDigit should be used to test one character. Test all characters to test if the string is a positive integer, e.g.
            Code:
            function isInteger(num) {
                for (i = 0; i < num.length; i++) {
                    var char = num.charAt(i);
                    if (!isDigit(char)) return false;
                }
                return true;
            }

            Comment

            • lolodede
              New Member
              • Apr 2009
              • 63

              #7
              this is the script i have but i want to add the intger shouldnt be more than 100 ie between 0-100
              Code:
              <script type="text/javascript">
              <!--
              function checkNum(number)
              {
              if (isNaN(number.value) )
                {
                  alert(number.value + " is not a number. Please re-enter.");
                  number.focus();
                  number.select();
                }
                return;
              }
              Last edited by acoder; Apr 4 '09, 10:53 AM. Reason: Added [code] tags

              Comment

              • lolodede
                New Member
                • Apr 2009
                • 63

                #8
                i have this code but i want to put more than 1000 is not accepted
                Code:
                function checkNum(number)
                { 
                if (isNaN(number.value) )
                  {
                    alert(number.value + " is not a number. Please re-enter.");
                    number.focus();
                    number.select();
                  }
                  
                  return;
                }
                Last edited by acoder; Apr 4 '09, 10:53 AM. Reason: Added [code] tags

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  isNaN will be false for float numbers too, e.g. 1.23, but you only want positive integers.

                  Anyway, to check if the number is not greater than 1000, see post #2.

                  Comment

                  Working...