正则表达式的javascript用户输入验证不起作用

时间:2018-11-19 06:53:11

标签: javascript jquery

我正在尝试使我的regexp和ajax函数正常工作,但是没有运气。 我想匹配,如果用户在tinymce中键入“ @”字符,然后将其插入到jquery帖子中以获取所需的结果,但是正则表达式不起作用,因为它返回null值,而且我想知道是否存在在spl.char“ @”之后键入时验证用户输入的方法,因为我似乎无法在第一个用户输入字符串之后读取脚本(我不知道该怎么办,因为我正在摸索个小时的时间来完成这项工作),谁能帮助我,并告诉我我要去哪里错了。 我到目前为止尝试过的代码:

function load_Ajax(uname){
    $.post("jsdropdown.php",{"uname":uname},function(res){
        alert(res);
    });
}
tinymce.init({
  selector: 'textarea#wall_id_1',
  height: 300,
  theme: 'modern',
  resize: false,
  force_p_newlines: false,
  plugins: 'print preview powerpaste searchreplace autolink directionality advcode visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount tinymcespellchecker a11ychecker imagetools mediaembed  linkchecker contextmenu colorpicker textpattern help',
  toolbar1: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent  | removeformat',
  image_advtab: true,
  templates: [
    { title: 'Test template 1', content: 'Test 1' },
    { title: 'Test template 2', content: 'Test 2' }
  ],
  content_css: [
    '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
    '//www.tinymce.com/css/codepen.min.css'
  ],

  init_instance_callback: function(editor) {
        $(editor.contentDocument.activeElement).atwho({at: "@", data: names});

  },
      setup: function(editor) {
        editor.on('keyup', function(e) {

            var eli= $(editor.contentDocument.activeElement).prop('innerHTML');

            var txt=$(eli+'p').html();
          var txt1=$(txt+'br').html();
          var regex=new RegExp(/@+([a-zA-z!._-]+)/g);
           var match= regex.exec(txt1); 
     //console.log(match);
     alert(match);
  load_Ajax(match);
          if(e.keyCode == 13 && $(editor.contentDocument.activeElement).atwho('isSelecting'))
            return false;
        });
      }
 });

更新:- 按照@QubaishBhatti的建议,我将match var更改为regex.exec(txt),脚本可以完美读取所有用户输入值。新的问题是我似乎无法将ajax响应传递给数组并将其传递给at类(我对代码进行了很多更改,但无法获得获取名称并将其传递给class的预期结果)当我提醒结果var时,得到响应“ [{"readyState":4,"responseText":"\n\n{\"names\":\"Shikhar Bansal(bshikhar13131313@gmail.com),\"}","responseJSON":{"names":"Shikhar Bansal(bshikhar13131313@gmail.com),"},"status":200,"statusText":"OK"}]”。 我到目前为止尝试过的代码:

 tinymce.init({
  selector: 'textarea#wall_id_1',
  height: 300,
  theme: 'modern',
  resize: false,
  force_p_newlines: false,
  plugins: 'print preview powerpaste searchreplace autolink directionality advcode visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount tinymcespellchecker a11ychecker imagetools mediaembed  linkchecker contextmenu colorpicker textpattern help',
  toolbar1: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent  | removeformat',
  image_advtab: true,
  templates: [
    { title: 'Test template 1', content: 'Test 1' },
    { title: 'Test template 2', content: 'Test 2' }
  ],
  content_css: [
    '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
    '//www.tinymce.com/css/codepen.min.css'
  ],
  setup: function(editor) {
        editor.on('keyup', function(e) {
    /*      
            var eli= $(editor.contentDocument.activeElement).prop('innerHTML');

            var txt=$(eli+'p').html();
          var txt1=$(eli+'span').html();
          var regex=new RegExp(/@+([a-zA-z!._-]+)/g);
           var match= regex.exec(txt); 
     //console.log(match);
   // alert(match);
 var names=load_Ajax(match[1]);
 return names;*/
          if(e.keyCode == 13 && $(editor.contentDocument.activeElement).atwho('isSelecting'))
            return false;
        });
  return names;    
  },

  init_instance_callback: function(editor) {

          var results=new Array(); 
    var get_names= editor.on("keyup",function(e){

            var eli= $(editor.contentDocument.activeElement).prop('innerHTML');

            var txt=$(eli+'p').html();
          var txt1=$(eli+'span').html();
          var regex=new RegExp(/@+([a-zA-z!._-]+)/g);
           var match= regex.exec(txt); 


 $.ajax({
           url : "jsdropdown.php",
   data : {"uname":match[1]},
   async: false,
   complete:function(res){
      results.push(res);
      //results=res;
     // alert(JSON.stringify(res));
    //return res;
    },
    dataType: "json"
});
 /*var names=$.when(load_Ajax(match[1])).done(function(response){
     return response;
 });*/


   alert(JSON.stringify(results));  
    }).responseJSON;



        $(editor.contentDocument.activeElement).atwho({at: "@", data: names});

  }

 });

0 个答案:

没有答案