Javascript validation for multiple textboxes

前端 未结 4 1248
故里飘歌
故里飘歌 2021-01-24 07:12

I am having some trouble figuring out how to validate my textboxes using js. I have 10 textboxes, the user can fill out any number 1-10, but cant fill out 0. Here is the js th

4条回答
  •  故里飘歌
    2021-01-24 07:24

    You can get a collection of all these textboxes with document.getElementsByName. Then loop through them, and make sure at least one is filled in:

    var allTbs = document.getElementsByName("Student_ID");
    var valid = false;
    for (var i = 0, max = allTbs.length; i < max; i++) {
        if (allTbs[i].value) { 
           valid = true;
           break;
        }
    }
    

    DEMO

提交回复
热议问题