www.webdeveloper.com
Results 1 to 2 of 2

Thread: Validation of Dynamically created fields

  1. #1
    Join Date
    Jun 2013
    Posts
    1

    Post Validation of Dynamically created fields

    I have created some dynamic fields and want to make them required.
    I am using the following code and it works but I need to create a loop in case there are more than 2 fields

    Code:
    function validateForm()
    { 
       if( document.myForm.record_0.value == "" )
       {
         alert( "Please select a record series." );
         document.myForm.record_0.focus() ;
         return false;
       }
       if( document.myForm.record_1.value == "" )
       {
         alert( "Please select a record series." );
         document.myForm.record_1.focus() ;
         return false;
       }
       return( true );
    }

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,656
    haven't tested this code but hope it works

    Code:
    function validateForm(){
    var fields=document.getElementById('myForm').elements,valid=true;
    for(var i=0; i<fields.length; i++){
    if(fields[i].type!=='text'&&fields[i].tagName!=='textarea'){continue;}
    else{
    if(fields[i].value===''){alert('The focused field must not be empty');valid=false;break;}
    }
    }
    return valid;
    }

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