修复输入字段的验证

时间:2016-12-13 14:21:22

标签: javascript php html regex validation

我点击“添加”按钮后弹出一个对话框。有两个字段,MR ID和供应商ID。 MR ID是一个下拉列表,不需要任何类型的验证。供应商ID是文本输入,需要验证。它只需要数字,也可以没有2个相同的供应商ID。它们必须都是独一无二的。到目前为止,我所使用的代码无法验证供应商ID。

HTML / PHP对话框:

<div id="dialog-form" title="Add Supplier ID">
  <p class="validateTips">All form fields are required.</p>

<!-- Dialog box displayed after add row button is clicked -->
  <form>
    <fieldset>
      <label for="mr_id">MR_ID</label>
      <select name="mr_id" id="mr_id_dialog" class="text ui-widget-content ui-corner-all" value="300">
          <?php foreach($user->fetchAll() as $user1) { ?>
            <option>
                <?php echo $user1['MR_ID'];?>
            </option>
        <?php } ?>
      </select><br><br>
      <label for="supplier_id">Supplier ID</label>
      <input type="text" name="supp_id" id="supplier_id" class="text ui-widget-content ui-corner-all" value="99">

      <!-- Allow form submission with keyboard without duplicating the dialog button -->
      <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

JavaScript的:

    // ----- Dialog Box for adding supplier id -----

$(document).ready( function() {
    $("#insertButton").on('click', function(e){
    e.preventDefault();
  });   
    var dialog, form,
      mr_id_dialog = $( "#mr_id_dialog" ),
      supplier_id = $( "#supplier_id" ),
      allFields = $( [] ).add( mr_id_dialog ).add( supplier_id ),
      tips = $( ".validateTips" );

    function updateTips( t ) {
      tips
        .text( t )
        .addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }

    function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
      } else {
        return true;
      }
    }

   function addVendor() {
      var valid = true;
      allFields.removeClass( "ui-state-error" );
// ----- Validation for each input in add row dialog box -----
      //valid = valid && checkRegexp( mr_id_dialog, /^(0|[1-9][0-9]*)$/, "Please enter a valid MR ID" );
      valid = valid && checkRegexp( supplier_id, /^(0|[1-9][0-9]*)$/g, "Please enter a valid Supplier ID" );
      if ( valid ) {
        var $tr = $( "#index_table tbody tr" ).eq(0).clone();
        var dict = {};
        var errors = "";
        $.each(allFields, function(){
          $tr.find('.' + $(this).attr('id')).html( $(this).val()+"-"+supplier_id );
          var type = $(this).attr('id');
          var value = $(this).val();
          // ----- Switch statement that provides validation for each table cell -----
          switch (type) {
            case "mr_id_dialog":
                dict["MR_ID"] = value;
              break;
            case "supplier_id":
                dict["Supp_ID"] = value;
              break;
            }
        });
        $( "#index_table tbody" ).append($tr);
        dialog.dialog( "close" );
      }
    }

    var dialog = $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 400,
      width: 350,
      modal: true,
      buttons: {
        "Add Supplier ID": addVendor,
        Cancel: function() {
          dialog.dialog( "close" );
        }
      },
      close: function() {
        form[ 0 ].reset();
        allFields.removeClass( "ui-state-error" );
      }
    });

    form = dialog.find( "form" ).on( "submit", function( event ) {
      event.preventDefault();
      addVendor();
    });

    $( "#insertButton" ).button().on( "click", function() {
      dialog.dialog({
          position: ['center', 'top'],
          show: 'blind',
          hide: 'blind'
      });
      dialog.dialog("open");
    });
});

应该通过的样本:

349348
2
1234

不应传递的样本:

01234
123 45 67
No hyphens, dashes, etc. Numbers only.

0 个答案:

没有答案