我想验证ckeditor textarea,验证是否适用于除textarea之外的所有元素,为什么?

时间:2016-10-14 06:53:09

标签: php jquery html ckeditor

我想验证ckeditor textarea,验证是否适用于ckeditor的textarea之外的所有其他元素,为什么?

我使用jquery进行验证,使用php作为后端。 以下是我的代码,

我的jquery脚本

    <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

<script>
  $(function() {
    $("#register-form").validate({
        rules: {
            test: "required",
            subject: "required",
            editor1:"required",
            optiona: "required" ,
            optionb: "required",
            optionc: "required",
            optiond: "required",
            correct: "required" ,
            questionno1: "required"
        },
         messages: {
            test: "test is required",
            subject: "Subject is required",
            editor1: "Question is required",
            optiona: "Option a is required",
            optionb: "Option b is required",
            optionc: "Option c is required",
            optiond: "Option d is required",
            correct: "Answer is required",
            questionno1: "Question number is required"
        },
        submitHandler: function(form) {
            form.submit();
        }
    });
  });
  </script>

这是我的html表单

    <form class="form-horizontal" action="" method="POST" id="register-form" novalidate="novalidate">
    <div class="row form-mar-top">
    <div class="col-md-2"></div>
    <div class="col-md-8 testform">
    <div class="col-md-6">
    <div class="form-group">
        <label class="control-label col-sm-2">Test</label>
        <div class="col-sm-10">
        <input type="hidden" name="questionid" id="questionid" value="<?php echo $row["Id"];?>" class="error">
         <select class="form-control" name="test" id="test">
         <option value="<?php echo $row["Testno"];?>"><?php echo $row["Testno"];?></option>
          <option value="Test1" >Test 1</option>
          <option value="Test2" >Test 2</option>
           <option value="Test3" >Test 3</option>
            <option value="Test4" >Test 4</option>
             <option  value="Test5" >Test 5</option>
              <option  value="Test6" >Test 6</option>
              <option  value="Test7" >Test 7</option>
               <option value="Test8" >Test 8</option>
                <option  value="Test9" >Test 9</option>
                 <option  value="Test10">Test 10</option>
                 </select>
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-sm-2">Subject</label>
        <div class="col-sm-10"> 
          <input type="text" class="form-control" id="subject" name="subject" value="<?php echo $row["Subject"];?>" placeholder="Enter Subject" class="error">
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-sm-2">Question no.</label>
        <div class="col-sm-10">
                <input type="text" class="form-control" id="questionno1" name="questionno1" value="<?php echo $row["Questionno"];?>" placeholder="Question number" class="error">

        </div>
      </div>

      <div class="form-group">
        <label class="control-label col-sm-2">Question</label>
        <div class="col-sm-10">
              <textarea id="editor1" name="editor1" maxlength="50" class="error"><?php echo $row["Question"];?></textarea>
            <script type="text/javascript">
                    CKEDITOR.replace('editor1');
            </script>
        </div>
      </div>
    </div>
    <div class="col-md-6">
        <div class="form-group">
        <label class="control-label col-sm-2">Option A</label>
        <div class="col-sm-10"> 
          <input type="text" class="form-control" id="optiona" name="optiona" value="<?php echo $row["Option1"];?>" placeholder="" class="error">
        </div>
      </div>
          <div class="form-group">
        <label class="control-label col-sm-2">Option B</label>
        <div class="col-sm-10"> 
          <input type="text" class="form-control" id="optionb" name="optionb" value="<?php echo $row["Option2"];?>" placeholder="" class="error">
        </div>
      </div>
          <div class="form-group">
        <label class="control-label col-sm-2">Option C</label>
        <div class="col-sm-10"> 
          <input type="text" class="form-control" id="optionc"  name="optionc" value="<?php echo $row["Option3"];?>" placeholder="" class="error">
        </div>
      </div>
          <div class="form-group">
        <label class="control-label col-sm-2">Option D</label>
        <div class="col-sm-10"> 
          <input type="text" class="form-control" id="optiond" name="optiond" value="<?php echo $row["Option4"];?>" placeholder="" class="error">
        </div>
      </div>
            <div class="form-group">
        <label class="control-label col-sm-2">Correct Answer</label>
        <div class="col-sm-10"> 
          <select class="form-control" name="correct" id="correct" class="error">
           <option value="<?php echo $row["Answer"];?>"><?php echo $row["Answer"];?></option>
          <option value="OptionA" >Option A</option>
          <option value="OptionB" >Option B</option>
           <option value="OptionC" >Option C</option>
            <option value="OptionD" >Option D</option>
                 </select>
        </div>
      </div>
      <div class="form-group"> 
        <div class="col-sm-offset-3 col-sm-6">
          <button type="submit" name="submit" id="submit" class="btn btn-default">update</button>
          <span style="color:red"><?php echo $numErr; ?></span>
        </div>
      </div>
    </div>
    </div>
    <div class="col-md-2"></div>
    </div>
    </form>

2 个答案:

答案 0 :(得分:0)

添加

ignore: "",

之前的规则

$(function() {
    $("#register-form").validate({
        ignore: "",
        rules: {
            test: "required",
            subject: "required",
            editor1:"required",
            optiona: "required" ,
            optionb: "required",
            optionc: "required",
            optiond: "required",
            correct: "required" ,
            questionno1: "required"
        },
         messages: {
            test: "test is required",
            subject: "Subject is required",
            editor1: "Question is required",
            optiona: "Option a is required",
            optionb: "Option b is required",
            optionc: "Option c is required",
            optiond: "Option d is required",
            correct: "Answer is required",
            questionno1: "Question number is required"
        },
        submitHandler: function(form) {
            form.submit();
        }
    });
  });

答案 1 :(得分:0)

我对我的jquery脚本进行了更改,代码工作正常。

<script>


  $(function() {


    $("#register-form").validate({
       ignore: [],

        rules: {
            test: "required",
            subject: "required",
            editor1:{
                         required: function() 
                        {
                         CKEDITOR.instances.editor1.updateElement();
                        },


                    },
            optiona: "required" ,
            optionb: "required",
            optionc: "required",
            optiond: "required",
            correct: "required" ,
            questionno1: "required"
        },


        messages: {
            test: "test is required",
            subject: "Subject is required",
            editor1:{
                        required:"Please enter Text",



                    },
            optiona: "Option a is required",
            optionb: "Option b is required",
            optionc: "Option c is required",
            optiond: "Option d is required",
            correct: "Answer is required",
            questionno1: "Question number is required"
        },

        submitHandler: function(form) {
            form.submit();
        }
    });

  });

  </script>