2012-04-12 138 views
2

好的,我想用自定義規則驗證我的下拉菜單,並且不想將它內聯,因爲它需要是一個整數值。這意味着提交時不能有請選擇一個選項。所以我不知道我是否需要讓它停用,或者我該如何處理。驗證下拉菜單

<select id="sel1R3" class="chzn-done" name="sticky" style="display: none;"> 
    <option selected="selected" value="">Please Select An Option</option> 
    <option value="0">No</option> 
    <option value="1">Yes</option> 
</select> 

編輯:現在由於某種原因,其沒有顯示爲下拉菜單中的驗證錯誤。

的jQuery:

$(document).ready(function() 
{ 

$.validator.addMethod("valueNotEquals", function (value, element, arg) { 
    return arg != value; 
}, "Message to User"); 

/* 
* Validate the form when it is submitted 
*/ 
var validateform = $("#newArticleForm").validate({ 
    rules: { 
     title: { 
      required: true, 
      minlength: 5 
     }, 
     category: { 
      required: true, 
      {valueNotEquals: "-1"} 
     }, 
     sticky: { 
      required: true, 
      {valueNotEquals: "-1"} 
     }, 
     comments: { 
      required: true, 
      {valueNotEquals: "-1"} 
     }, 
     datetime: { 
      required: true, 
      date: true 
     }, 
     status: { 
      required: true, 
      {valueNotEquals: "-1"} 
     }, 
     file: { 
      required: true, 
      accept: 'png|jpe?g|gif' 
     }, 
     permalink: { 
      required: true, 
     }, 
     article: { 
      required: true, 
      minlength: 5 
     } 
    }, 
    invalidHandler: function(form, validator) { 
     var errors = validator.numberOfInvalids(); 
     if (errors) { 
      var message = errors == 1 
      ? 'You missed 1 field. It has been highlighted.' 
      : 'You missed ' + errors + ' fields. They have been highlighted.'; 
      $('.box .content').removeAlertBoxes(); 
      $('.box .content').alertBox(message, {type: 'warning', icon: true, noMargin: false}); 
      $('.box .content .alert').css({ 
       width: '100%', 
       margin: '0', 
       borderLeft: 'none', 
       borderRight: 'none', 
       borderRadius: 0 
      }); 
     } else { 
      $('.box .content').removeAlertBoxes(); 
     } 
    }, 
    showErrors : function(errorMap, errorList) { 
     this.defaultShowErrors(); 
     var self = this; 
     $.each(errorList, function() { 
      var $input = $(this.element); 
      var $label = $input.parent().find('label.error').hide(); 
      $label.addClass('red'); 
      $label.css('width', ''); 
      $input.trigger('labeled'); 
      $label.fadeIn(); 
     }); 
    }, 
    submitHandler: function(form) { 
     var dataString = $('#newArticleForm').serialize(); 
     $.ajax({ 
      type: 'POST', 
      url: 'dashboard/articleSubmit', 
      data: dataString, 
      dataType: 'json', 
      success: function(data) { 
       if (data.error) { 
        $('.box .content').removeAlertBoxes(); 
        $('.box .content').alertBox(data.message, {type: 'warning', icon: true, noMargin: false}); 
        $('.box .content .alert').css({ 
         width: '', 
         margin: '0', 
         borderLeft: 'none', 
         borderRight: 'none', 
         borderRadius: 0 
        }); 
       } 
       else 
       { 
        $('.box .content').removeAlertBoxes(); 
        $('.box .content').alertBox(data.message, {type: 'success', icon: true, noMargin: false}); 
        $('.box .content .alert').css({ 
         width: '', 
         margin: '0', 
         borderLeft: 'none', 
         borderRight: 'none', 
         borderRadius: 0 
        }); 
        $(':input','#newArticleForm') 
        .not(':submit, :button, :hidden, :reset') 
        .val(''); 
       } 
      } 
     }); 
    } 
}); 

}); 

HTML:

<div class="grid_6"> 
     <div class="box"> 
      <div class="header"> 
       <img src="http://www.kansasoutlawwrestling.com/kowmanager/assets/img/icons/packs/fugue/16x16/document--plus.png" alt="" width="16" height="16" /> 
       <h3>Create a News Article</h3> 
      </div> 
          <form method="post" accept-charset="utf-8" id="newArticleForm" enctype="multipart/form-data">     <div class="content no-padding"> 
        <div class="section _100"> 
         <label for="title">Title</label> 
         <div> 
          <input type="text" name="title" value="" />       </div> 
        </div> 

        <div class="section _100"> 
         <label for="category">Category</label> 
         <div> 
          <select name="category"> 
<option value="" selected="selected">Please Select An Opion</option> 
<option value="4">Columns</option> 
<option value="2">Headlines</option> 
<option value="1">Main News</option> 
<option value="3">Rumors</option> 
</select>       </div> 
        </div> 

        <div class="section _100"> 
         <label for="sticky">Is Sticky</label> 
         <div> 
          <select name="sticky"> 
<option value="-1">Please Select An Option</option> 
<option value="0">No</option> 
<option value="1">Yes</option> 
</select>       </div> 
        </div> 

        <div class="section _100"> 
         <label for="comments">Allow Comments</label> 
         <div> 
          <select name="comments"> 
<option value="-1">Please Select An Option</option> 
<option value="0">No</option> 
<option value="1">Yes</option> 
</select>       </div> 
        </div> 

        <div class="section _100"> 
         <label for="datetime">Date Comments Expire</label> 
         <div> 
          <input id="datetime" type="datetime" name="datetime" /> 
         </div> 
        </div> 

        <div class="section _100"> 
         <label for="status">Status</label> 
         <div> 
          <select name="status"> 
<option value="-1">Please Select An Option</option> 
<option value="0">Inactive</option> 
<option value="1">Active</option> 
</select>       </div> 
        </div> 

        <div class="section _100"> 
         <label for="file">Image</label>       <div> 
          <input type="file" name="file" value="" />       </div> 
        </div> 

        <div class="section _100"> 
         <label for="permalink">Permalink</label> 
         <div> 
          <input type="text" name="permalink" value="" />       </div> 
        </div> 

        <div class="section _100"> 
         <label for="article">Article</label> 
         <div> 
          <textarea name="article" cols="30" rows="5" id="article" ></textarea>       </div> 
        </div> 
       </div><!-- End of .content --> 

       <div class="actions"> 
        <div class="actions-left"> 
         <input type="reset" name="reset" value="Reset" id="reset" />      </div> 

        <div class="actions-right"> 
         <input type="submit" name="submit" value="Submit" id="submit" />      </div> 
       </div><!-- End of .actions --> 
      </form>   </div><!-- End of .box --> 
    </div><!-- End of .grid_6 --> 

任何其他的想法?

編輯:

我在jQuery的文檔到處找,找不到如何正確地做到這一點。

+1

你在問我們什麼? – Marc 2012-04-12 23:35:16

+0

早些時候有一個答案,由於某種原因,這個人刪除它,但我問爲什麼它不報告下拉的驗證錯誤。 – 2012-04-12 23:38:17

+0

這似乎並不需要一種自定義方法;您只需使用「整數」和「必需」規則,並在「請選擇一個選項」選項中使用空值屬性。這隻有在a)某物被選中並且b)某物是一個整數時纔會通過。 – 2012-04-13 00:29:25

回答

6

指定自定義的驗證規則正確的方法是這樣的:

category: { 
    required: true, 
    valueNotEquals: "-1" 
} 

話雖這麼說,你不必創建自定義規則來處理這個問題。只需使用required:true並確保選項value =「」爲默認選項,jQuery Validate將爲您處理它。

我已經創建了example供您查看,我將status下拉列表更改爲使用默認方法,並且我修復了您的其他自定義驗證方法引用以顯示它的工作方式(這是@El Yobo建議)。我還修復了永久鏈接對象中的尾隨逗號(這會使整個事情在許多IE版本中不起作用)。

+0

什麼是固定鏈接對象?此外,我想弄清楚如何我可以做永久文本框,如說wordpress做它取出空格,並把破折號和小寫大寫字母等。 – 2012-04-13 16:49:09

+0

對不起,驗證調用中的永久鏈接定義 - 它看起來像'permalink:{required:true,}',裏面有一些額外的新行。對於您的固定鏈接替換,您可以使用正則表達式來用空白替換空格,並且JavaScript String對象具有可以完成該部分的toLowerCase()方法。在頂部右側的'submitHandler'中執行此操作。 – Ryley 2012-04-13 17:39:51

+0

嗯,我希望有這樣做,一旦我把焦點從標題框中刪除時,已經放置在該字段中 – 2012-04-13 18:48:40