开发者

How to limit the character number in bracket using javascript

开发者 https://www.devze.com 2023-01-29 15:39 出处:网络
Hi i have this this text. [If you purchased or upgraded RegexBuddy after 13 June 2007, you can download the latest version by ][typing in your email address and RegexBuddy user ID below.]

Hi i have this this text.

[If you purchased or upgraded RegexBuddy after 13 June 2007, you can download the latest version by ][typing in your email address and RegexBuddy user ID below.]

Right now the the open and close bracket indicate it is 100 and below characters (using javascript).

How do i prevent user from enter other words between the bracket that will make it more than 100 chars?

I dont want to prevent user from input anything, i just want to prevent the sentence in the bracket will not exceed 100 chars.开发者_开发百科

Thank you.


Quick 'n' dirty:

$(someElementOrSelector).keydown(function(e) {
    var bracket_start = $(this).text().indexOf('['),
        bracket_end = $(this).text().lastIndexOf(']');
    if((bracket_end - bracket_start) > 100) {
       e.preventDefault();
       return false;
    }
});


I think you can try to match 100 character in bracket if yu have a match you should display on alert or what ever is convenient to you.

if (myString.match(/\[.{100}[^\]]+\]/))
{
   alert('you have too many characters'); 
}


you can put a function on keypress event that count the length of the particular div, and then will check if that counter > 100 then will popup message will displayed and return false.

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消