validating a number entered in a textfield

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foss
    New Member
    • Dec 2007
    • 11

    validating a number entered in a textfield

    Hi friends,
    My form have a text field named "price" which should have only number and if it contains decimal number then it should not have more than two numbers after decimal. I tried this but it is not working . If the data entered is valid (as described above) then it should post to the target.php .....

    I hope u will point out my mistake OR u may advice some wise alternative for this ..
    thanks.....
    The code goes as follows.....

    [HTML]<html>
    <head>
    <script type="text/javascript">
    function checkForm()
    {
    // var price = document.form1. price;
    document.form1. submit();

    var value=document. getElementById( 'price').value;
    var indx=value.inde xOf('.');
    var digit=value.sub string(indx+1);

    if(digit.length >2)
    {
    alert('only two decimal digit allowed');
    document.form1. price.focus();
    price.select();
    document.form1. price.focus();
    return false;
    }
    else
    {
    document.form1. submit();

    }
    }
    </script>
    </head>
    <body>
    <form name="form1" id="price" method="post" action="print selected area.html" >

    <input name="price" type="text" id="price" size="25" maxlength="16" />

    <input type="button" name="submit" value="ADD" onsubmit="chkFo rm()"/>
    </form>
    </body>
    </html>
    [/HTML]
    Last edited by gits; Dec 27 '07, 08:38 AM. Reason: added code tags
  • foss
    New Member
    • Dec 2007
    • 11

    #2
    Sorry to disturb u friends .
    I got the solution to the problem . I has made some silly mistakes
    the solution is

    [HTML]<html>
    <head>
    <script type="text/javascript">


    function check_price()
    {
    // var price = document.form1. price;
    var value=document. getElementById( 'price').value;
    var indx=value.inde xOf('.');
    var digit=value.sub string(indx+1);

    if (isNaN(value)== true )
    {
    alert("Please enter a valid number");
    document.form1. price.focus();
    price.select();
    document.form1. price.focus();
    return false;
    }

    else if(digit.length >2)
    {
    alert('Only two decimal digits allowed');
    document.form1. price.focus();
    price.select();
    document.form1. price.focus();
    return false;
    }
    else
    {
    document.form1. submit();
    }


    }



    </script>
    </head>
    <body>
    <form name="form1" method="post" action="control _center.php" >

    <input name="price" type="text" id="price" size="25" maxlength="16" />

    <input type="button" name="submitBut ton" value="ADD" onFocus="check_ price()"/>
    </form>
    </body>
    </html>[/HTML]
    Last edited by gits; Dec 27 '07, 08:39 AM. Reason: added code tags

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by foss
      Sorry to disturb u friends .
      I got the solution to the problem . I has made some silly mistakes
      the solution is
      price.select() would result in an error because the line defining price at the beginning of the function has been commented out.

      Comment

      Working...