Form Validation with Javascript on a "Dynamic" Form

MiracleManSMiracleManS Chambersburg, PA Icrontian
edited June 2005 in Internet & Media
Here's one for savvy amongst us.

Right now I'm working on a form that is built using a PHP foreach loop. The name s of the form items are dynamic, in that they are qty_$itemid. What i need to do is ensure that what is put into the qty_$itemid field is, indeed, a number. I was hoping to use Javascript instead of another function in PHP. Any tips or suggestions on how I can finish this?

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited June 2005
    I'm not sure if this is what you're looking for, but this tests an argument (string) to make sure all the characters are numbers
    function isNum(argvalue) {
         argvalue = argvalue.toString();
    
         if (argvalue.length == 0)
              return false;
    
         for (var n = 0; n < argvalue.length; n++)
              if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
                   return false;
    
         return true;
    }
    
  • MiracleManSMiracleManS Chambersburg, PA Icrontian
    edited June 2005
    shwaip wrote:
    I'm not sure if this is what you're looking for, but this tests an argument (string) to make sure all the characters are numbers
    function isNum(argvalue) {
         argvalue = argvalue.toString();
    
         if (argvalue.length == 0)
              return false;
    
         for (var n = 0; n < argvalue.length; n++)
              if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
                   return false;
    
         return true;
    }
    


    That's pretty much what I was hoping for, lucky thing for me I found another way to do it by simply running the "eval" function on the string. Here's the code:
    function checknumber_qty(idno){
              
    eval("var x=document.orderform.qty_" + idno + ".value")
    
    var anum=/(^\d+$)|(^\d+\.\d+$)/
         
         if (anum.test(x))
                testresult=true
         
         else{
                 alert("Please input a valid number in the QTY field")
                 testresult=false
         }
    
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited June 2005
    cool. javascript is by no means my forté - and your way's probably better. Hell - I don't understand it, so it must be :p
  • MiracleManSMiracleManS Chambersburg, PA Icrontian
    edited June 2005
    What it does is call the eval function built into javascript. Essentially it forces the string to be evaluated as if it were an actual command, as opposed to just a string. So it allows you to tack onto a string at will, more or less.
  • KyleKyle Lafayette, LA New
    edited June 2005
    isNaN() is a built-in JavaScript function that returns true if the argument is not a number, false otherwise. I see you're using a regular expression to accomplish this part, but using isNaAN() is probably much easier to maintain.

    The eval() function can be useful and often seems like the best solution, but it is also very very slow compared to every other Javascript function. There are many articles out there urging Javascript developers to fully explore all possible solutions before using it. Keep in mind that Javascript is client-side scripting, so the runtime for you is different than Joe Schmoe on a PII.

    Your problem is that the form elements have ID#s in their names and you don't know these numbers?

    If you know what # they start and end at, try this:
    for(i = startNum; i <= endNum; i++) {
    	tempString = "qty_" + i;
    	if( isNaN(orderform.elements[tempString].value) ) {
    		alert("Please input a valid number in the QTY field");
    		testresult = false;
    	}
    }
    
    Or, if every input element in that particular form needs to be validated, you could do this:
    for(i = 0; i < orderform.elements.length; i++) {
    	if( isNaN(orderform.elements[i].value) ) {
    		alert("Please input a valid number in the QTY field");
    		testresult = false;
    	}
    }
    
    Don't get me wrong. Eval is nice. It works great. It's probably not slow enough for you or most of your site's visitors to notice. But if you have lots of these fields to check, a hundred evals or so could get ugly.
  • MiracleManSMiracleManS Chambersburg, PA Icrontian
    edited June 2005
    That's a great point, but for what I was doing I had no idea what the ID numbers may or may not have been or starting position. It's almost like having a random number tacked on. Unless someone decides to order 100 items at once (something that isn't going to happen on campus), it won't be too much of a slow down for anyone.

    I might try your suggestion, just to see if it might work, but I'm not sure how to work it out if I'm not sure what number is tacked on (as I have to take that unique number and apply it to the item to update the information in the database).
  • KyleKyle Lafayette, LA New
    edited June 2005
    Ah I see. I really just wanted to suggest that so you could know to avoid eval() down the line. I also try to avoid putting unique IDs in form element names (like you were doing) because it just begs for complications. Instead of having description_ID, quantity_ID, etc associated with a unique product ID just start at 0 or 1 with your numbering and store the ID in a hidden form field with that same number: textBox_001, checkBox_001, id_001
Sign In or Register to comment.