如何使用jquery.validationEngine插件驗證CLEditor wysiwyg編輯器輸入?我正在嘗試使用引用的特定jquery插件驗證表單提交的html編輯器輸入。如何使用jquery驗證引擎來驗證CLEditor(html編輯器)輸入
2
A
回答
1
這是我想出的解決方案。您還可以從pastebin
< - 請PLEASE,投票,如果它是有幫助的副本。
// Include all the jquery, jquery ui, cleditor, and validation-engine CSS and javascript library files here
<script type="text/javascript">
// object used to store the validation states of any html editor textarea inputs used on the page
var editorStates = new Object();
// add one property/value for each textarea id that you are using on this page..
// in this example there are going to be three diferent editor inputs:
editorStates['your_textarea_id_1']=true;
editorStates['your_textarea_id_2']=true;
editorStates['your_textarea_id_etc']=true;
$(document).ready(function(){
// ==========================================================================
// initialize the cleditor object(s) for the textarea(s) used on this page...
// ==========================================================================
// initialize the 'your_textarea_id_1' textarea and store the cleditor object in the 'your_textarea_id_1_editor' variable...
var your_textarea_id_1_editor = $("#your_textarea_id_1").cleditor({
width: 650, // width not including margins, borders or padding
height: 220, // height not including margins, borders or padding
controls: // controls to add to the toolbar
"bold italic underline | font size " +
"style | color highlight removeformat | bullets numbering | outdent " +
"indent | alignleft center alignright justify | pastetext source",
}).change(function(){
if(editorStates['your_textarea_id_1']==false){ // input has been validated as false so need to check it as user types so the flag goes away when appropriate...
valid_editor_text('your_textarea_id_1'); // re-validate this input
};
});
// initialize the 'your_textarea_id_2' textarea and store the cleditor object in the 'your_textarea_id_2_editor' variable...
var your_textarea_id_2_editor = $("#your_textarea_id_2").cleditor({
width: 650, // width not including margins, borders or padding
height: 220, // height not including margins, borders or padding
controls: // controls to add to the toolbar
"bold italic underline | font size " +
"style | color highlight removeformat | bullets numbering | outdent " +
"indent | alignleft center alignright justify | pastetext source",
}).change(function(){
if(editorStates['your_textarea_id_2']==false){ // input has been validated as false so need to check it as user types so the flag goes away when appropriate...
valid_editor_text('your_textarea_id_2'); // re-validate this input
};
});
// initialize the 'your_textarea_id_etc' textarea and store the cleditor object in the 'your_textarea_id_etc_editor' variable...
var your_textarea_id_etc_editor = $("#your_textarea_id_etc").cleditor({
width: 650, // width not including margins, borders or padding
height: 220, // height not including margins, borders or padding
controls: // controls to add to the toolbar
"bold italic underline | font size " +
"style | color highlight removeformat | bullets numbering | outdent " +
"indent | alignleft center alignright justify | pastetext source",
}).change(function(){
if(editorStates['your_textarea_id_etc']==false){ // input has been validated as false so need to check it as user types so the flag goes away when appropriate...
valid_editor_text('your_textarea_id_etc'); // re-validate this input
};
});
// ==========================================================================
// initialize the jquery validation-engine
// ==========================================================================
// Note: 'mainform' is the id value of the form element that contains the three textarea inputs
// Replace this with the id value of YOUR form id!
jQuery("#mainform").validationEngine('attach', {
onValidationComplete: function(form, status){
// Note: 'status' will already be true/false based on the validation results of any other inputs that you
// are validating using the typical validation methods provided by the validationEngine plugin.
// Now we need to validate the textarea (cleditor html editor) inputs...
// validate the 'your_textarea_id_1' textarea input
if(your_textarea_id_1_editor[0].sourceMode() == false){
// in editor mode, need to update the hidden textarea input
your_textarea_id_1_editor[0].updateTextArea();
}
if(! valid_editor_text('your_textarea_id_1')){ // <-- pass the textarea id value to the 'valid_editor_text' function
// The validation on this input failed...
status=false; // prevent the form from submitting
}
// validate the 'your_textarea_id_2' textarea input
if(your_textarea_id_2_editor[0].sourceMode() == false){
// in editor mode, need to update the hidden textarea input
your_textarea_id_2_editor[0].updateTextArea();
}
if(! valid_editor_text('your_textarea_id_2')){ // <-- pass the textarea id value to the 'valid_editor_text' function
// The validation on this input failed...
status=false; // prevent the form from submitting
}
// validate the 'your_textarea_id_etc' textarea input
if(your_textarea_id_etc_editor[0].sourceMode() == false){
// in editor mode, need to update the hidden textarea input
your_textarea_id_etc_editor[0].updateTextArea();
}
if(! valid_editor_text('your_textarea_id_etc')){ // <-- pass the textarea id value to the 'valid_editor_text' function
// The validation on this input failed...
status=false; // prevent the form from submitting
}
// submit if there are no validation errors
if(status == true){
form.validationEngine('detach'); // prevents an endless loop
form.submit(); // form is valid, now submit it
}
}
});
}); // end doc ready
// ============================================================================
// The 'valid_editor_text' function
// This function does the actual validation of the textarea (html editor) input
// ============================================================================
function valid_editor_text(inputID){
str = $('#'+inputID).val();
// Note: str contains the value of the textarea input so do whatever validations you need
// against that. Return true if it's valid or false if it isn't.
// Right now I am just checking to make sure that the user entered anything at all ...
// remove any <br>, <br />, , {spaces}, and/or {newlines} from the beginning of the string
// just to make sure the user typed some actual text instead of random spaces and enter keys (aka nothing)
str = str.replace(/^(?:<[Bb][Rr](?: \/)?>|\&[Nn][Bb][Ss][Pp];|\n|)+/g,'');
if(str.length > 0){
console.log("valid_editor_text('"+inputID+"')"+' returning true');
editorStates[inputID]=true;
// hide any previous prompts on this input
$('#'+inputID+'_prompt_location').validationEngine('hide');
return true;
}else{
console.log("valid_editor_text('"+inputID+"')"+' returning false');
editorStates[inputID]=false;
// need to display the validation message for this input
$('#'+inputID+'_prompt_location').validationEngine('showPrompt', 'This field is required.', 'required', 'topRight', true);
return false;
}
}
</script>
</head>
<body>
<form action="youraction.goes.here" method="post" name="mainform" id="mainform">
<!-- Add a placeholder div that is used to target the position of a validation message if the validation fails
The id value is the value of your textarea id with '_prompt_location' appended, like so.. -->
<div>
<div id="your_textarea_id_1_prompt_location"></div>
<!-- Finally, the textarea input that is causing all this fuss .. -->
<textarea id="your_textarea_id_1" name="your_textarea_id_1"></textarea>
</div>
<!-- repeat for the other textareas you are using .. -->
<div>
<div id="your_textarea_id_2_prompt_location"></div>
<textarea id="your_textarea_id_2" name="your_textarea_id_2"></textarea>
</div>
<div>
<div id="your_textarea_id_etc_prompt_location"></div>
<textarea id="your_textarea_id_etc" name="your_textarea_id_etc"></textarea>
</div>
<p>Here is a standard text input to demo how typical validation works along with our custom ..<br />
<input type="text" name="a_standard_input" id="a_standard_input" value="" class="validate[required]" />
</p>
<p>
<input type="submit" name="b1" value="Submit this form!" />
<!-- Note: the validation occurs when the form is submitted via submit button or javascript (not shown) -->
</p>
</form>
</body>
</html>
2
一個簡單的辦法:閱讀validationengine文檔現在
,說要防止表單被提交,如果文本大小超過65536個字符:
<script type="text/javascript">
jQuery(document).ready(function(){
// binds form submission and fields to the validation engine
jQuery("#newForm").validationEngine();
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#textArea").cleditor({
// cleditor options here
});
});
</script>
<form id="newForm" name="newForm" method="post" action="yourphppage.php" onSubmit="return validatesize()">
<textarea id="textArea" name="textArea"></textarea>
<input name="submit" id="button" type="submit" value="Submit">
</form>
<script type="text/javascript">
function validatesize() {
submitFlag=true;
if(document.newForm.textArea.value.length>65536){
submitFlag=false;
$("#textArea").validationEngine('showPrompt', 'Custom message', 'red', 'bottomLeft')
}
return submitFlag;
}
</script>
不要忘記將腳本放在HEAD部分:
<link rel="stylesheet" type="text/css" href="jquery.cleditor.css" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.cleditor.js"></script>
<link rel="stylesheet" href="validationEngine.jquery.css" type="text/css" media="screen" />
<script type="text/javascript" src="jquery.validationEngine.js"></script>
<script type="text/javascript" src="jquery.validationEngine-en.js"></script>
相關問題
- 1. 禁止「與jQuery驗證引擎輸入
- 2. jQuery驗證引擎驗證搜索按鈕點擊輸入還
- 3. jQuery驗證引擎
- 4. 如何使用jQuery驗證引擎驗證組?
- 5. 如何設置類輸入錯誤使用jQuery驗證引擎
- 6. jquery驗證引擎 - 驗證json列表
- 7. jQuery的驗證引擎驗證
- 8. jquery和jQuery驗證引擎
- 9. 驗證HTML編號輸入
- 10. 我如何驗證TINYMCE編輯器與jQuery驗證
- 11. 如何將jQuery的驗證引擎驗證器應用於jQuery TokenInput
- 12. 使用Jquery驗證輸入
- 13. jQuery Select2&驗證引擎
- 14. 使用jquery驗證引擎master驗證星級評分
- 15. 使用MsDropDown與jQuery驗證引擎
- 16. 使用jQuery驗證引擎定位
- 17. ASP.NET MVC驗證 - 如何更改MVC驗證引擎輸出的html?
- 18. jQuery驗證引擎的自定義驗證器
- 19. 如何使用JavaScript來驗證HTML編輯空間
- 20. jQuery驗證輸入
- 21. jQuery驗證:容器和HTML驗證
- 22. 如何在Javascript(Jquery)中定義驗證器函數來驗證輸入字段?
- 23. Jquery驗證'require'不能使用html編輯器
- 24. 如何使用jQuery驗證驗證recaptcha_tag?
- 25. 調用驗證選項卡單擊(jQuery驗證引擎)
- 26. 如何使用jQuery驗證器插件驗證多個輸入類型文件
- 27. ace編輯器語法驗證html + javascript
- 28. 使用富文本編輯器時的輸入驗證
- 29. 驗證使用jQuery驗證
- 30. jquery驗證引擎:使用java(不使用php)驗證ajax字段
暗示我發佈的解決方案是在沒有閱讀文檔的情況下創建的,這很荒謬。你的解決方案是原始的,並執行這樣的。我會堅持我發佈的公認解決方案。 – Drew
我沒有暗示任何東西。我的解決方案很簡單,順便說一句,它的工作原理。 –
導致提示的問題得到糾正後,它不會刪除提示。它也盲目地認爲隱藏的textarea與WYSIWYG編輯器顯示的內容保持同步,事實並非總是如此。所以是的,它的工作原理。 – Drew