Ace编辑器:特定行的代码验证

时间:2016-11-22 10:31:15

标签: javascript jquery json validation ace-editor

我正在研究讲座验证码。我希望能够检查一行,获取内容并验证它,如果它是用户应该放置的。我正在使用" for loop"迭代过程。 JSON设置验证过程。问题是返回错误不匹配。我的问题是使用Ace编辑器验证代码的最佳方法是什么?

验证流程(JSON)

  {
"course" : "html",
"count" : 2,
"line" : [8, 9],
"check" : ["My name is", ""],
"error" : ["Please type your name. Start with 'My name is'","Write why  you want to learn HTML"]
}

HTML&的Javascript



var html = ace.edit("html-editor");
 html_option = {
    mode: "ace/mode/javascript",
    theme: "ace/theme/monokai",
    fontSize: "10pt",
    showPrintMargin : false,
    enableBasicAutocompletion: true,
    enableSnippets: true,
    enableLiveAutocompletion: true,
    wrap: true,
    scrollPastEnd: true,
    displayIndentGuides: true,
  };

 html.setOptions(html_option);

//Get the JSON validation file
var fond = $(".json").html();
      fond = JSON.parse(fond);

//Begin validation on Click "Run"
$("#run").on("click", function(){
        $('#run').html('<i class="fa fa-spinner fa-spin"></i> Running');
        
        var fond_course = fond.course;
        var fond_count = fond.count;
        
        //Begin error check
        for (var i = 0; i < fond_count; i++) {
          var line = fond.line[i] - 1;
          var error = fond.error[i] ;
          var check = fond.check[i];
          
          var fond_check = html.session.getLine(line);
          
          var patt = new RegExp(check);
            var res = patt.test(fond_check);
          
          //If a specific line is empty
          if(check === null){
            html.gotoLine(line);
            line = line + 1;
            
            //Error function with parameters
            //error_note(true, error, line);
          }
          //If a line is not equal to the JSON file value
          else if(!res){
            html.gotoLine(line);
            line = line + 1;
            
           //Error function with parameters
           //error_note(true, error, line);
            
          }else if(res && check !== null){
            //Code is validated, now save the code
          }
        }
      });
&#13;
#html-editor{
height: 500px; 
}

#run{
font-size: 20px;
  padding: 10px;
  cursor: pointer;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.5/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.5/ext-language_tools.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="run">Run</button>
<pre id="html-editor">&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
     &lt;title&gt;Introduction to HTML&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Welcome to Code&lt;/h1&gt;
        &lt;p&gt;Edit and replace with your name&lt;/p&gt;
        
    &lt;/body&gt;
&lt;/html&gt;<pre>
&#13;
&#13;
&#13;

0 个答案:

没有答案