validation for dynamic multiple text fileds

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • infernodeep
    New Member
    • May 2008
    • 3

    validation for dynamic multiple text fileds

    hi guys,im new to this forum
    could anyone help me to solve this problem for me..

    In my site im generating multiple text boxes and i need to wirte validations for that....
    Code:
    <form id="form1" name="form1" method="post" action="">
      <input name="cc1" type="text" id="cc1" value="0" />
      <br />
    <input name="cc2" type="text" id="cc2" value="0" />
    <br />
    <input name="cc3" type="text" id="cc3" value="0" />
    <br />
    <input name="cc4" type="text" id="cc4" value="0" />
    <br />
    <input name="" type="submit" onclick="return valid()" />
    </form>
    Code:
    function emptyvalid(elem,altxt){
    var txt = document.getElementById(elem).value;
    if(txt == ""){
    alert(altxt);
    return false;
    }
    return true;
    }
    function valid(){
    for(i=1;i<10;i++){
    if(emptyvalid("cc"+i,"not empty")==false){
    return false;
    }
    }
    }
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    Uses the element names not IDs:
    Code:
    <input name="" type="submit" onclick="return valid(this.form,'cc','Empty')" />
    ...........
    function valid(formRef, prefix, warnText)
    {
      for(var i=1, elem, found=false; (elem=formRef[ prefix+i ]) && !found;   i++)
       if( !elem.value.match(/\S/) )
       {
    	 alert( prefix + i +'\n\n' + warnText );
    	 found=true;
       }
    
     return !found;
    }

    Comment

    • infernodeep
      New Member
      • May 2008
      • 3

      #3
      Originally posted by Logician
      Uses the element names not IDs:
      Code:
      <input name="" type="submit" onclick="return valid(this.form,'cc','Empty')" />
      ...........
      function valid(formRef, prefix, warnText)
      {
        for(var i=1, elem, found=false; (elem=formRef[ prefix+i ]) && !found;   i++)
         if( !elem.value.match(/\S/) )
         {
      	 alert( prefix + i +'\n\n' + warnText );
      	 found=true;
         }
      
       return !found;
      }
      thank u Logician....... your code works fine....thank so muuch

      Comment

      Working...