weixin_33721344 2017-02-03 01:17 采纳率: 0%
浏览 15

验证连续字段

I am building a webform which offers the possibilty to add more article fields dynamically. The form is valid if either the person leaves a message or fills in the one row of articlespecifications:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

<form>
  <table class="articlelist">
    <tbody>
      <tr id="articleheaders">

      </tr>

      <tr>
        <td>
          <input type="text" name="form_SGTIN_CODE_1" class="codefield" id="form_SGTIN_CODE_1" maxlength="8"/>
        </td>
        <td>
          <input type="text" name="form_SGTIN_CODE_2" class="codefield" id="form_SGTIN_CODE_2" maxlength="8"/>
        </td>
        <td>
          <input type="text" name="form_SGTIN_CODE_3" class="codefield" id="form_SGTIN_CODE_3" maxlength="8" value="1"/>
        </td>
        <td>
          <input type="checkbox" name="form_SGTIN_CODE_4" class="codecheckbox" id="form_SGTIN_CODE_4" />
        </td>
      </tr>

    </tbody>
  </table>

  <span id="addArticle">Hinzufügen</span>
  <p>
    <textarea id="form_MESSAGE" name="form_MESSAGE"></textarea>
  </p>
  <?php echo $errorMessage; ?>
  <br />
  <input type="submit" />
</form>


</body>


<script>
var currentfieldnumber = 4;
$('#addArticle').on('click', function(event) {
  $('<tr>'+
  '<td>'+
  '<input type="text" name="form_SGTIN_CODE_' + currentfieldnumber +'" class="codefield" id="form_SGTIN_CODE_' + currentfieldnumber++ +'" maxlength="8"/>'+
  '</td>'+
  '<td>'+
  '<input type="text" name="form_SGTIN_CODE_' + currentfieldnumber +'" class="codefield" id="form_SGTIN_CODE_' + currentfieldnumber++ +'" maxlength="8"/>'+
  '</td>'+
  '<td>'+
  '<input type="text" name="form_SGTIN_CODE_' + currentfieldnumber +'" class="codefield" id="form_SGTIN_CODE_' + currentfieldnumber++ +'" maxlength="8" value="1"/>'+
  '</td>'+
  '<td>'+
  '<input type="checkbox" name="form_SGTIN_CODE_' + currentfieldnumber +'" class="codecheckbox" id="form_SGTIN_CODE_' + currentfieldnumber++ +'" />'+
  '</td>'+
  '<td style="padding-left:15px !important;">'+
  '<span class="deleteRowBtn">X<span>'+
  '</td>'+
  '</tr>').insertAfter('#articleheaders');
});


//delete the row of the corresponding button
$(document).on('click', '.deleteRowBtn',  function(event) {
   $(this).parent().parent().remove();
});

//prevent from entering letters into codefields
$(document).on('keypress', '.codefield', function(event) {
  if (!(event.keyCode >= 48 && event.keyCode<=57) && !(event.keyCode>=37&&event.keyCode<=40)){
   event.preventDefault();
  }
});
</script>


</html>

The validation I am using on the webform is done via ajax and a mix of jquery. Here is the essential part:

  function isEmpty(&$value){
    return !isset($value) || $value == "";
  }
    $articleflag=false;
    if(!isEmpty($param['form_MESSAGE'])){
      $articleflag = true;
    }
    foreach ($param as $key => $value) {
      if(strpos($key, 'form_SGTIN_CODE_')!==false){
        $flag = true;
        $num = substr($key, 16);
        for ($i=$num; $i <= $num +2 ; $i++) {
          if(isEmpty($param['form_SGTIN_CODE_'.$i])){
            $flag = false;
          }
        }
        if($flag){
          $articleflag = true;
          break;
        }
      }
    }
    if(!$articleflag){
      $valid = false;
      array_push($errFields, 'form_MESSAGE');
    }

Things which are important: The fields get passed as a $param array. The Checkbox which is generated does not have to be checked in order for a row to be valid.

Is there any smart way to do this? Link to the fiddle: LINK

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      悬赏问题

      • ¥15 PADS Logic 原理图
      • ¥15 PADS Logic 图标
      • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
      • ¥20 气象站点数据求取中~
      • ¥15 如何获取APP内弹出的网址链接
      • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部