Extend the TextArea property, combined with Jquery.validate.js, and validate the length of the textarea at the same time as submitting the form.
1.jQuery plug-in for TEXTAREA length verification
Copy Code code as follows:
Verify the length of the textarea
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: remove prompts when keyboard input is in the correct range
}
Note:
1) Parameter pass:
Defaults = {
min:0
, Max:5
}
Used to receive the shortest and maximum length of the textarea, respectively.
2) return value
True: Verify length through
False: Verify length failed
2. Instructions for use:
Add a JS reference to the page:
<script type= "Text/javascript" src= ". /.. /.. /scripts/jquery/jquery.textarea.js "></script>
Example:
In the page
<textarea id= "Txtcontent" rows= "4" cols= "></textarea>"
<button id= "Chklen" >check Textarea length</button>
When the button event is triggered, we can judge the length of the textarea.
Copy Code code as follows:
$ ("#chklen"). Click (function () {
var bool = $ ("#txtContent"). Checklength ({
Min:-1
, Max:10
});
if (bool) {
alert (bool);
}
});
If the textarea is in form forms, the form is submitted and the validation is done, and the condition is satisfied.
The following example:
Copy Code code as follows:
Determine whether the Testarea length exceeds the limit
var ckcontent = $ ("#txtContentIntro"). Checklength ({
Min:-1//Don't judge whether it is empty
, max:512//Maximum length 512
});
Form validation
var B = $ ("#fcourseware"). valid ();
Perform upload operation, save courseware information after successful upload
if (b && ckcontent) {
Todo:submit form
}
Min and max two parameters can not be passed a value, the default minimum length of 0, the maximum length of 10. If the textarea is not required, the Min value is given-1.
Finally, the operation is done by returning the bool value.