textbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vagos

    #1

    textbox

    Can someone please, tell me how to check the value of a textbox in a form,

    1, the value must be only a positive number ( not 34ax43) , for input a
    stock number of product
    2, the value must be positive komma and two digits max (eg 150,20) , for
    input price value


    Thank you,
    Vagos
    Thessaloniki
    Greece


  • kaeli

    #2
    Re: textbox

    In article <1109866123.924 844@athnrd02>, vagos@freemail. gr enlightened us
    with...[color=blue]
    > Can someone please, tell me how to check the value of a textbox in a form,
    >
    > 1, the value must be only a positive number ( not 34ax43) , for input a
    > stock number of product
    > 2, the value must be positive komma and two digits max (eg 150,20) , for
    > input price value
    >
    >[/color]

    Modify this.

    function checkNumeric(st rObject)
    {
    /* Returns true if the field has all numeric characters, false if not.
    You must pass in a input (text) object, not the value.
    Usage:
    if (checkNumeric(f ormname.textbox name))
    */
    var re = /^\d+$/;
    if (!strObject || isBlank(strObje ct)) return false;
    else return re.test(strObje ct.value);
    }


    --
    --
    ~kaeli~
    He had a photographic memory that was never developed.



    Comment

    • Grant Wagner

      #3
      Re: textbox

      "vagos" <vagos@freemail .gr> wrote in message
      news:1109866123 .924844@athnrd0 2...[color=blue]
      > Can someone please, tell me how to check the value of a textbox in a
      > form,
      >
      > 1, the value must be only a positive number ( not 34ax43) , for input
      > a
      > stock number of product[/color]

      Since you don't say how many digits, you could use something like this:

      /^\d+$/

      If you the range of the number of digits (say 5 to 8 digits), you could
      use:

      /^\d{5,8}$/
      [color=blue]
      > 2, the value must be positive komma and two digits max (eg 150,20) ,
      > for
      > input price value[/color]

      /^\d+,\d{0,2}$/

      You'd use all of them in a validation routine:

      <form ... onsubmit="valid ate(this);">

      <script type="text/javascript">
      function validate(f)
      {
      var msg = [], ctrl;
      if (!/^\d+$/.test(f.element s['product_id'].value))
      {
      msg.push('- product id is wrong');
      if (!ctrl)
      {
      ctrl = f.elements['product_id'];
      }
      }
      if (!/^\d+,\d{0,2}$/.test(f.element s['price'].value))
      {
      msg.push('- price is wrong');
      if (!ctrl)
      {
      ctrl = f.elements['price'];
      }
      }
      if (msg.length > 0)
      {
      alert(
      'The following problems were' +
      ' found and must be corrected:\n\n' +
      msg.join('\n')
      );
      if (ctrl && ctrl.focus)
      {
      ctrl.focus();
      }
      return false;
      }
      return true;
      }
      </script>

      A design something like this puts all the errors in one place when the
      user tries to submit the form, rather then telling them the product id
      is wrong, then they correct that and try to submit again and they're
      told the price is wrong, etc.

      The whole -ctrl- thing is designed to place the focus on the first
      incorrect form control.

      Don't forget to do all this validation on the server as well.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq


      Comment

      • Dr John Stockton

        #4
        Re: textbox

        JRS: In article <1109866123.924 844@athnrd02>, dated Thu, 3 Mar 2005
        18:17:32, seen in news:comp.lang. javascript, vagos <vagos@freemail .gr>
        posted :[color=blue]
        >Can someone please, tell me how to check the value of a textbox in a form,
        >
        >1, the value must be only a positive number ( not 34ax43) , for input a
        >stock number of product
        >2, the value must be positive komma and two digits max (eg 150,20) , for
        >input price value[/color]

        See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

        You mean, for 1, a positive integer; but you may need to allow 0.

        OK = /^\d{1,6}$/.test(thing.val ue) // allows 0..999999
        OK = /^[1-9]\d{0,2}$/. ... // should allow 1..999
        OK = /^\d+,\d\d$/. ... // for 0.00 to 9.....9,99

        Inadequately tested above.

        If the least price exceeds 0.99 the price RegExp can force a leading
        digit of 1-9 as in the second line; otherwise check +thing.value > 0,
        unless you do free offers.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...