The length of the JQuery textarea is validated _jquery

Source: Internet
Author: User
Tags extend
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.