sayedsohail Posted October 1, 2007 Share Posted October 1, 2007 HI everyone, I got six variable cn1 to cn6, would like to check the value of the fields using for loop and alert the user if any of the cn1 to cn6 fields are empty here is my code? How do i convert this into for loop? if ((cn1.value =="") || (cn1.value == null) || isblank(cn1.value) || (cn2.value =="") || (cn2.value == null) || isblank(cn2.value) ) { alert("Please enter all the required fields"); } Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted October 1, 2007 Share Posted October 1, 2007 are you using id tags for the fields? you could just do something like this <input id="cn1" name="cn1" type="text" /> <script> //if you're using id attribute for(var i=0;i<6;i++){ document.getElementById("cn"+i).value } ////if you're using name attribute for(var i=0;i<6;i++){ document.getElementByName("cn"+i).value } </script> Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted October 1, 2007 Author Share Posted October 1, 2007 var cn1 = document.getElementById('cname').value; var cn2 = document.getElementById('address1').value; This is how i am storing the value to cn1 to cn6 But how do i check the value for all the 6 ellements and than alert the user, if any of the fields are empty? Please. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 1, 2007 Share Posted October 1, 2007 var fields = [ document.getElementById('cname').value, document.getElementById('address1').value ]; var error = false; var error_msg = ""; for(c in fields){ if(fields[c] == ''){ error = true; error_msg = fields[c].name + ' is blank.\n'; } } if(error){ alert(err_msg); } Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 1, 2007 Share Posted October 1, 2007 there were some errors with my orig post var fields = [ document.getElementById('cname'), document.getElementById('address1') ]; var error = false; var error_msg = ""; for(c in fields){ if(fields[c].value == ''){ error = true; error_msg += fields[c].name + ' is blank.\n'; } } if(error){ alert(err_msg); } #edit or better yet, give each field the title attribute and put in a descriptive field title like title="User Name" and change this to error_msg += fields[c].title + ' is blank.\n'; Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted October 1, 2007 Author Share Posted October 1, 2007 var fields = [document.getElementById('cname').value, document.getElementById('address1').value, document.getElementById('postcode').value, document.getElementById('city').value, document.getElementById('phone').value, document.getElementById('mobile').value ]; var error = false; var error_msg = ""; // this is what i changed but its not working// for(c in fields){ if(fields[c] == '' || fields[c] == null || isblank(fields[c])){ error = true; error_msg = fields[c].name + ' is blank.\n'; } } if(error){ alert(err_msg); } else { Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 1, 2007 Share Posted October 1, 2007 with null you'll need the === operator as it is a datatype not a string or integer isblank() a custom function? and use my correction where i took the value out of the defining array so that you can access more of the element's properties inside of the loop Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted October 1, 2007 Author Share Posted October 1, 2007 I kept the original revised code , but still its not working although the fields are there. var fields = [ document.getElementById('cname'), document.getElementById('address1') ]; var error = false; var error_msg = ""; for(c in fields){ if(fields[c].value == ''){ error = true; error_msg += fields[c].name + ' is blank.\n'; } } if(error){ alert(err_msg); } Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 1, 2007 Share Posted October 1, 2007 if(error){ alert(err_msg); } to if(error){ alert(error_msg); } Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted October 1, 2007 Author Share Posted October 1, 2007 million thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.