Javascript Form Validation With Array Values
I will be brief, but try to give you a good idea of what I need through a smaller example.
I have 3 boxes of information: one text, one starting time and one ending time.
Every day someone will input some "activities" they did throughout the day. For instance "Mowed the Lawn" (text input), Starttime=10:00am (select input) - Endtime=11:00am (select input).
However I have built into this webpage the ability to click on a button and it will add another 3 inputs (activity, starttime, endtime) for all the activities they did through the day. If it helps, I have this structured so that the name of these inputs are for example activity[], starttime[], endtime[] so that PHP will render it correctly and store the values appropriately in the database.
My goal is that I don't want to put bad time data into my database (starttime = 10:00am, endtime = 9:00am. I understand how to do form validation for 1 instance of the 3 inputs (getElementById has done this for me), but not multiple instances of the three inputs in a dynamic array.
Thoughts开发者_运维技巧?
Here's a link to a screenshot (since I'm a new user and can't post images). http://admissions.utils.us/images/Screenshot.png
you could check to see how many elements on the page have the tag and then do a for loop to cycle through the array of input elements and then do your verification
var inputs = document.getElementsByTagName('input').length;
for (var i = 0; i < inputs; i++){
if (document.getElementsByTagName('input')[i].value != ''){
return true;
} else {
return false;
}
}
精彩评论