SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    I have a small shopping card which has text fields named
    quantity_4, quantity_10... (quantity_{orderid_here})
    Customer can update quantity fields and then update form by clicking on update button. But then I wanna read all quantity fields, but there are more fields than only quantity.
    So it should be something like this I think
    for (i=0;i<this.form.items.count-1;i++){
    myfield=this.form.items(i)
    if insrt(myfield.name with "quantity_")
    check every char at myfield.value
    }

    I am so bad at javascripting so 2 main things i need
    to get collection of formfields(how?) and then compare fieldname with a string(how?)
    thanks for help
    Great Dane
    Gokhan ARLI

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi gokhana,

    There are many ways to go about checking form elements. The one I use is as follows:

    function chkForm(formObj)
    {
    with (formObj)
    {
    for (i = 0; i < length; i++)
    {
    if (elements[ i].type = 'text')
    {
    fldVal = elements[ i].value;
    ...other processing including checking for elements[ i].name (quantity)...
    }
    if (elements[ i].type = 'select')
    {
    selObj = elements[ i];
    for (j = 0; j < selObj.length; j++)
    if (selObj.options[ j].selected)
    fldVal = selObj.options[ j].value;
    ...other processing...
    }
    }
    }
    }

    If you are interested in more along this line, see the "Validating Forms" script/tutorial at my site.

    Insofar as checking only quantity fields, you can loop them with the following:

    for (i = 0; i < maxNumOfQty; i++)
    {
    fldObj = eval('document.formName.quantity_' + i);
    ...other processing.....
    }

    Hope this helps

    Vinny

    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey Vinny
    thank you very much it worked well with the help of you and I learned lots of things with this small javascript code(case sensitivity, variables in functions are global....)
    if anybody interested checkform code is below
    function checkform(FormObj)
    {
    with (FormObj)
    {
    for (z = 0; z < length ; z++)
    {
    if (elements[z].type == 'text')
    {
    if (elements[z].value == '')
    {
    alert("The text box is empty.")
    elements[z].focus()
    return false
    }
    if (strLeft(elements[z].name,'_') == "quantity")
    {
    astr = elements[z].value
    if (IsInteger(astr)==false)
    {
    alert("Value of this box has to be a number")
    elements[z].focus()
    return false
    }
    }
    }
    }
    }
    return true;
    }
    Great Dane
    Gokhan ARLI

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Gokhan,

    Glad to have been of help. If you would like to expand your javascript knowledge, see the javascript primer at: http://htmlgoodies.earthweb.com It's well-written, geared for newbies, and amusing (at times

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

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