ckeditor validation for spaces

拥有回忆 提交于 2020-07-15 09:59:12

问题


How can i implement validation on ckeditor to prevent user from adding spaces only . Any answer within today will be greatly appreciated .

following is what i tried for validations so far :

//Save note from ckeditor
    $("input.save_note").click(function()
    {  
        var note_id1 = $(this).attr("rel");
        var thiss1 = this;
        var profile_user_id = $(this).attr("rel1");
        var txt=CKEDITOR.instances.editor1.getData();
        var no_space_txt = txt.replace(" ","");
//      var txt = txt1.replace(/ +(?= )/g,'');
        var editor_val1 = CKEDITOR.instances.editor1.document.getBody().getChild(0).getText() ;
        var editor_val = editor_val1.replace(/ +(?= )/g,'');
//      if( editor_val == "")
//      {
//          alert('sak');
//          return;
//      }
        if(editor_val !=="" && editor_val !=="")
        {
        $(this).attr("disabled","disabled");
        var loading_img = addLoadingImage($(this),"before");
        jQuery.ajax({
            url: "/" + PROJECT_NAME + "profile/save-note-in-editor",
            type: "POST",
            dataType: "json",
            data: { "profile_user_id" : profile_user_id , "note" : txt },
            timeout: 50000,
            success: function(jsonData) {
                if(jsonData){
                    $("p#clickable_note_"+note_id1).hide();
                    $(thiss1).hide();
                    $(thiss1).siblings("input.edit_note").fadeIn();  
                    $("span#"+loading_img).remove();
                        $(".alert-box").remove();
                        $(".alert-box1").remove();
                        $(".alert-box2").remove();
                        showDefaultMsg( "Note saved successfully.", 1 );
                        $(thiss1).removeAttr('disabled');
//                      $(".cke_inner cke_reset").hide();
                        CKEDITOR.instances.editor1.destroy();
                        $(".editor1").hide();
                        $("p#clickable_note_"+note_id1).html(jsonData);//to display the saved note in clickable p tag .
                        $("p#clickable_note_"+note_id1).fadeIn('slow');// To display the note paragraph again after editing and saving note.
                    }
                    else{
                        $(thiss1).removeAttr('disabled');
                        $(thiss1).before('<span class="spanmsg" id="note-msg" style="color:red;">Server Error</span>');
                    }
                },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
         });
        }
        else
        {
            alert("You cannot make an empty note. Please insert some text.");

        }

    });

I have implement alert to check if no text is entered but i want to check if user only enter spaces . please suggest some accurate way .


回答1:


Since you are evidently using jQuery, you can add jQuery.trim() as an additional check for your condition:

jQuery.trim(editor_val).length != 0

This will trim all whitespace from the submitted form and check whether there are any characters remaining. If the input consists only of whitespace, the statement will evaluate to false. You would integrate it into the following line:

if (editor_val != "" && editor_val != "" && jQuery.trim(editor_val).length != 0)


来源:https://stackoverflow.com/questions/21846952/ckeditor-validation-for-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!