简体   繁体   English

组合Java验证功能

[英]Combining Javascript Validation Functions

Alright I need help combining the two JavaScript Functions... I have tried multiple times and am not coming up with any luck. 好吧,我需要结合这两个JavaScript函数的帮助...我已经尝试了多次,并且没有遇到任何麻烦。 There almost identical functions except the fact that I change one number so that it thinks there different textboxes. 除了我更改一个数字以使它认为存在不同的文本框外,几乎所有功能都相同。 I tried putting a variable in its place but then it always only validates to the ending number of the loop. 我尝试将变量放在其位置,但之后它始终仅验证到循环的结束号。 Please show me how I may be able to combine these two functions. 请告诉我如何将这两个功能结合起来。 (Its my only work around and I can not find any examples similar to mine) (这是我唯一的解决方法,找不到与我类似的任何示例)

First: 第一:

<script type="text/javascript">
var QnoText = ['abc_1'];  // add IDs here for questions with optional text input

function doSubmit_1() {
  var ids_1 = '';
  flag_1 = true;
    for (i=0; i<QnoText.length; i++) {
      CkStatus = document.getElementById(QnoText[i]).checked;
      ids_1 = QnoText[i]+'Certificate_1';
      if (CkStatus && document.getElementById(ids_1).value == '') {
        alert('Please enter certificate number 1.');
        document.getElementById(ids_1).focus();
        flag_1 = false;
        alert('return flag_1');
  }
 }
return flag_1;
}

</script>

Second: 第二:

<script type="text/javascript">
var QnoText = ['abc_2'];  // add IDs here for questions with optional text input

function doSubmit_2() {
  var ids_2 = '';
  flag_2 = true;
    for (i=0; i<QnoText.length; i++) {
      CkStatus = document.getElementById(QnoText[i]).checked;
      ids_2 = QnoText[i]+'Certificate_2';
      if (CkStatus && document.getElementById(ids_2).value == '') {
        alert('Please enter certificate number 2.');
        document.getElementById(ids_2).focus();
        flag_2 = false;
        alert('return flag_2');
  }
 }
return flag_2;
}

</script>

You can pass a parameter in your function with the number of the textbox, like this: 您可以在函数中传递带有文本框编号的参数,如下所示:

var QnoText = ['abc_2'];  // add IDs here for questions with optional text input

function doSubmit(n) {
  var ids = '';
  flag = true;
    for (i=0; i<QnoText.length; i++) {
      CkStatus = document.getElementById(QnoText[i]).checked;
      ids = QnoText[i]+'Certificate_' + n;
      if (CkStatus && document.getElementById(ids).value == '') {
        alert('Please enter certificate number ' + n + '.');
        document.getElementById(ids).focus();
        flag = false;
        alert('return flag_' + n);
  }
 }
return flag;
}

doSubmit(1); // for your submit 1
doSubmit(2); // for your submit 2

Is this what you wanted? 这就是你想要的吗? because is not very clear. 因为不是很清楚。 If is not feel free to explain. 如果不是随意解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM