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 2 of 2
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Paraguay
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question "Building" field names?

    Please can anybody tell me how to do this:

    I'm working with a shopping cart and for every item selected from a catalogue, the user must enter the quantity.
    The problem I have is, due to the fact that the quantity field changes its name for every product selected, how can I
    validate the content of the field in Javascript (e.g. >0, only numbers, etc)?.
    I've tried to "build" the name of the fields in JS scripts, but it doesn't work.
    I'm attaching an example of my JS:

    Code:
    <script language="JavaScript">
    // num is the name of the field in html form
    
    function verify_qty(num)
    {
    var name_field='document.form_cart'+ num;
    
    var regex = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
      if (!regex.test(name_field.value)) {
      alert('You can only enter numbers here.');
      name_field.focus();
    
    }
    </script>
    Thanks,

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Use square bracket notation. (See my sig for more info)

    Code:
    var name_field = document.nameOfTheForm.elements["form_cart" + num];


  •  

    Posting Permissions

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