Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    restricting the dynamicallly generated text boxes from accepting more than one dot.

    I am generating the text boxes dynamically, and the generated text boxes can accept only numbers[0-9] and singele dot.
    how can i achive this....? below is the function which accepts only numbers....but how can i restrict the text box from

    accepting more than one dot.


    <html>
    <head>
    <script language="javascript">


    function numbersonly(eventKey)
    {
    var unicode=eventKey.keyCode;
    if (unicode!=8)
    {
    if (unicode<48||unicode>57)
    return false
    }

    }
    </script>
    </head>
    <body>
    <p>I am generating the text boxes dynamically, and the generated text boxes can accept only numbers[0-9] and singele dot.
    how can i achive this....? below is the function which accepts only numbers....but how can i restrict the text box from

    accepting more than one dot.
    </p>
    <input type="text" id="txtPlan1" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
    <input type="text" id="txtPlan2" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
    <input type="text" id="txtPlan3" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
    <input type="text" id="txtPlan4" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
    <input type="text" id="txtPlan5" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
    <input type="text" id="txtPlan6" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
    <input type="text" id="txtPlan7" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
    <input type="text" id="txtPlan8" name = "txtPlan" onkeypress="return numbersonly(event);"></br>

    <p>
    all the above text fields should accept only single dot.., in my program i am generating these text boxes dynamically</P>
    </body>

  • #2
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This might work:

    Code:
     function numbersonly (num, eventKey) {
      var unicode=eventKey.keyCode;
      var inp = document.getElementById('txtPlan'+num);
      if (unicode!=8) return (48<=unicode && unicode <=57);
      if (unicode==8) {
        for (var i=0; i++; i<inp.value.length) {
          if (inp.value.charAt(i) == '.') return false
        }
        return true;
      }
    }
    Of course you would need to change your inputs to:
    Code:
    <input type="text" id="txtPlan1" name = "txtPlan"  onkeypress="return numbersonly(1,event);"></br>
    <input type="text" id="txtPlan2" name = "txtPlan"  onkeypress="return numbersonly(2,event);"></br>
    <input type="text" id="txtPlan3" name = "txtPlan"  onkeypress="return numbersonly(3,event);"></br>
    <input type="text" id="txtPlan4" name = "txtPlan"  onkeypress="return numbersonly(4,event);"></br>
    <input type="text" id="txtPlan5" name = "txtPlan"  onkeypress="return numbersonly(5,event);"></br>
    <input type="text" id="txtPlan6" name = "txtPlan"  onkeypress="return numbersonly(6,event);"></br>
    <input type="text" id="txtPlan7" name = "txtPlan"  onkeypress="return numbersonly(7,event);"></br>
    <input type="text" id="txtPlan8" name = "txtPlan"  onkeypress="return numbersonly(8,event);"></br>
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    Try this:-

    function numbersonly(lstr) {
    if (/((\.+)\d*(\.+))|([^0-9\.])/gi.test(lstr.value)) {
    alert ("Invalid format - only digits and one single dot are allowed!");
    return false;
    }
    return true;
    }

    OR a simpler version:-
    if (/(\.+.*\.+)|([^0-9\.])/gi.test(lstr.value)) {

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
    Last edited by Philip M; 01-16-2007 at 07:33 AM.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    This may do it as well


    str="123.321"

    re=/^\d*\.?\d*$/

    alert(re.test(str))
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by Mr J View Post
    This may do it as well


    str="123.321"

    re=/^\d*\.?\d*$/

    alert(re.test(str))
    Yes, that works as well! And shorter!


  •  

    Posting Permissions

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