www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Input value should match array or alert

  1. #1
    Join Date
    Nov 2009
    Posts
    268

    resolved [RESOLVED] Input value should match array or alert

    NEED INPUT VALUES TO MATCH ARGS ARRAY OR THROW AN ALERT
    This is what I have but I want the alert to refrence the var args= or throw the alert.
    Code:
    function qtymultiply(qty){
    var fs=qty.parentNode.parentNode;
    var subtotal=document.getElementById('subtotal');
    var args=[1,2,4,6,8,10,20]; //array I need to match foreach element
    var multiply=0.0;
    var price=fs.cells[4].innerHTML;
    var quantity=fs.cells[5].getElementsByTagName('input')[0].value; 
    var cost=fs.cells[6];
    var rowsubtotal=fs.cells[7];
    if(qty.value!=''||qty.value!=0){ //qty.value '0' not throwing alert? Need to match args elements
    multiply+=price*quantity;
    cost.innerHTML=parseFloat(multiply).toFixed(2);
    sumsubtotal();
    rowsubtotal.innerHTML=subtotal.innerHTML;
    }
    else {
    alert('You only have these options: 1,2,4,6,8,10 or 20');
    qty.focus;
    }
    }
    Any help would be
    Last edited by THEFOOL; 02-06-2010 at 03:32 PM.

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    the input value is a STRING, not a NUMBER

    You need to use parseInt(yourString, 10);

    Eric

  3. #3
    Join Date
    Nov 2009
    Posts
    268

    Input value should match array or alert

    A1ien51

    Yeh A1. Reg.'Exprecien' Matching is why my JS is lowsey. 'PARSEINIT' converts 'args=[]' array toString then, can any value entered in the input that dosen't match it, ie '!=', throw the error? Something like this
    Code:
    function qtymultiply(qty){
    var fs=qty.parentNode.parentNode;
    var subtotal=document.getElementById('subtotal');
    var args=[1,2,4,6,8,10,20]; //array I need to match foreach element
    var multiply=0.0;
    var price=fs.cells[4].innerHTML;
    var quantity=fs.cells[5].getElementsByTagName('input')[0].value; 
    var cost=fs.cells[6];
    var rowsubtotal=fs.cells[7];
    if(qty.value != parseInit(args, 20) ){ //qty.value '0' not throwing alert? Need to match args elements
    multiply+=price*quantity;
    cost.innerHTML=parseFloat(multiply).toFixed(2);
    sumsubtotal();
    rowsubtotal.innerHTML=subtotal.innerHTML;
    }
    else {
    alert('You only have these options: 1,2,4,6,8,10 or 20');
    qty.focus;
    }
    }
    Last edited by THEFOOL; 02-07-2010 at 10:53 AM.

  4. #4
    Join Date
    Nov 2009
    Posts
    268

    Input value should match array or alert

    A1ien51
    Not sure that your asking me to parseInit the 'qty.value' back toString before comparing it to 'args.toString()' ?
    Something like this:
    Code:
    var args=[1, 2, 4, 6, 8, 10, 20];
    var x= args.toString();
    var y = qty.value.toString();
    if(y!= x){ //Would mean that 1,2,4,6,8,10,20 would have to be entered into 'qty' before an alert wasn't fired, 
    //wouldn't it? With out setting a specific index of the strings to be matched. Or would js search the whole 
    //length of both strings for any one element matched?
    Will try it any How
    Last edited by THEFOOL; 02-07-2010 at 11:38 AM.

  5. #5
    Join Date
    Nov 2009
    Posts
    268

    Thumbs up Input value should match array or alert

    Andrew_g
    at About.com Javascript Forum gets credit for this one. Thanks for the schooling Dude! You Rock!

    Code:
    function itemrowMultiply() {
    var aObj=document.getElementsByClassName('qty');
    for(var i=0; i<aObj.length; i++) {
        aObj[i].onchange=function() {qtymultiply(this);};
        }
    };
    function qtymultiply(qty){
        var fs=qty.parentNode.parentNode;
        var subtotal=document.getElementById('subtotal');
        var args=[1, 2, 4, 6, 8, 10, 20];
        var multiply=0.0;
        var price=fs.cells[4].innerHTML;
        var quantity=fs.cells[5].getElementsByTagName('input')[0].value;
        var cost=fs.cells[6];
        var rowsubtotal=fs.cells[7];
        for(var i=0; i !==args.length; i++) {
            if(qty.value ==args[i]){
                multiply+=price*quantity;
                cost.innerHTML=parseFloat(multiply).toFixed(2);
                sumsubtotal();
                rowsubtotal.innerHTML=subtotal.innerHTML;
                return; // exit loop and function.
            }
        }
      
    // This should only run if the statements in the if statement nested in the for loop do not execute.
        alert('You only have these options: 1, 2, 4, 6, 8, 10 or 20         ');
        qty.focus;
        cost.innerHTML='0';
        qty.value='!!';
        sumsubtotal();
    }
    Now all I need is a function that prevents the customer from continueing if the qty.value remains '!!'

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles