Extend the textarea attribute and combine it with jquery.validate.js to verify the length of the textarea while submitting the form. 1.jQuery plug-in to verify textarea length
//Verify textarea length jQuery.fn.checkLength = function(parameters) { defaults = { min: 0 , max: 5 } jQuery. extend(defaults, parameters); //The value of the current textarea var taValue = $(this).val(); var len = taValue.length; if (len >= defaults.max) { $(this).parent().append(showLengthError("max")).show(); window.setTimeout(function() { $(".lenError ").hide(); }, 5000); return false; } else if (len <= defaults.min) { $(this).parent().append (showLengthError("min")); window.setTimeout(function() { $(".lenError").hide(); }, 5000); return false; } else { return true; } //todo: When the keyboard input is within the correct range, eliminate the prompt }
comment : 1) Parameter passing: defaults = { min: 0 , max: 5 } are used to receive the minimum and maximum length of the textarea respectively . 2) Return value true: Verification length passed false: Verification length failed 2. Instructions for use: Add js reference to the page:
Example: In page
When the button event is triggered , we can judge the length of textarea.
$("#chklen").click(function( ){ var bool = $("#txtContent").checkLength({ min : -1 ,max: 10 }); if(bool){ alert(bool); } });
If the textarea is in the form, it will be verified when the form is submitted, and then submitted when the conditions are met. . The following example:
// Determine whether the testarea length is Exceeding the limit var ckContent = $("#txtContentIntro").checkLength({ min : -1 //Does not judge whether it is empty ,max: 512 //Maximum length 512 }) ; // Form validation var b = $("#fcourseware").valid(); // Perform upload operation and save courseware information after successful upload if (b && ckContent ) { // todo: submit form }
Both the min and max parameters do not need to pass a value. The default minimum length is 0 and the maximum length is 10. If textarea is not required item, then the min value is assigned -1. Finally, the operation is performed by returning a bool value.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn