Results 1 to 6 of 6

Thread: Prevent user from entering in the same number in the text box

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2002
    Location
    Hampton Beach
    Posts
    513

    Prevent user from entering in the same number in the text box

    I want to prevent a user from entering in all of the same number. Example the user enters in all 2, IE: 22222222222
    I want to display a msg.

    How can I prevent the user from entering in all of the same numbers?

    Note the user can enter in the same number more than once but the value in the txt box cannot contain only that one number

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function check_this(_obj) {
    first_number = _obj.value.substr(0,1)
    number_of_them = 0
    for (i=0; i<_obj.value.length; i++)
    	{
    	if (_obj.value.substr(i,1) == first_number)
    		{
    		number_of_them++
    		}
    	}
    if (number_of_them == _obj.value.length)
    	{
    	alert("cannot contain all the same number")
    	_obj.value = ""
    	}
    }
    </script>
    </head>
    <body>
    <input type="text" value="22222" id="mytxtx" onBlur="check_this(this)">
    </body>
    </html>
    Have I helped you? Please Rate my posts.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's quicker if you don't search the entire string.
    Code:
    var first = value.substr(0,1);
    for(var i = 1; i < value.length; ++i) {
      if(value[i] != first) {
        break;
      }
    }
    if(i == value.length) {
      alert("Everything the same");
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's quicker if you don't search the entire string.
    Code:
    var first = value.substr(0,1);
    for(var i = 1; i < value.length; ++i) {
      if(value[i] != first) {
        break;
      }
    }
    if(i == value.length) {
      alert("Everything the same");
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2002
    Location
    Hampton Beach
    Posts
    513
    Works fine only problem is if you leave the value empty you still get the same err msg. How can I prevent this? This is reference to the first solution by Acidic

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    just insert if IF like:
    Code:
    if (_obj.value.length==0)
      {
      alert("You must enter numbers in this field")
      }
    else {
      //Do the rest
      }
    But I would use CB solution, it's quicker (and shorter code). You can still use the same IF statement I just gave.
    Have I helped you? Please Rate my posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width