如何将警报消息添加到文本最大的javascript [英] How put alert message to text maximum javascript

查看:82
本文介绍了如何将警报消息添加到文本最大的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我对我的代码有疑问。我的问题是如果那个人超出限制词,我想提出警告信息。我的代码运行良好,当超过那个人无法输入时,无论哪个部分都在javascript我想弹出警报消息,如果他/她超过?



我尝试过:



Hello. I have a question regarding my codes. My question is "i want to put alert message if that person exceed words limitation". my code is function well and when exceed that person cannot type, however which part on javascript I want to put popup the alert message if he/she exceed?

What I have tried:

<textarea class="max" name="coverLetterText" id="coverLetterText" cols="80" rows="12" background placeholder="Make a Pitch not more than 400 words">

<pre><!-- script untuk textarea max-->  
<script>jQuery(document).ready(function($) {
    var max = 400;
    $('textarea.max').keypress(function(e) {
        if (e.which < 0x20) {
            // e.which < 0x20, then it's not a printable character
            // e.which === 0 - Not a character
            return;     // Do nothing
        }
        if (this.value.length == max) {
            e.preventDefault();
        } else if (this.value.length > max) {
            // Maximum exceeded
            this.value = this.value.substring(0, max);
        }
    });
}); //end if ready(fn)</script>

推荐答案

){
var max = 400;
) { var max = 400;


('textarea.max')。keypress(function(e){
if(e.which< 0x20){
// e.which< 0x20 ,那么它不是一个可打印的字符
// e.which === 0 - 不是字符
返回; //什么都不做
}
if(this.value.length == max){
e.preventDefault();
}否则if(this.value.length> max){
//最大值超过
this.value = this .value.substring(0,max);
}
});
}); //结束如果准备好(fn)< / script>
('textarea.max').keypress(function(e) { if (e.which < 0x20) { // e.which < 0x20, then it's not a printable character // e.which === 0 - Not a character return; // Do nothing } if (this.value.length == max) { e.preventDefault(); } else if (this.value.length > max) { // Maximum exceeded this.value = this.value.substring(0, max); } }); }); //end if ready(fn)</script>


现在,您的 this.value.length> 400 比较检查字符的数量,而不是单词(这样的检查也可能有意义 - 但是这个数字会更大,而且与单词检查的数量无关)。所以你必须用单词分割字符串并比较单词数组的长度:

Right now, your this.value.length > 400 comparison checks the number of characters, and not words (such a check probably makes sense too -- but then the number would be bigger and that's not related to the number of word check). So you have to split up the string in words and compare the length of the array of words:
var words = this.value.split(/\W+/);
if (words.length > max) {
    e.preventDefault();
} else if (words.length > max) {
    alert("You can only have 400 words.");
    return; // 'exits' the current function
}



split 用分隔符分割JavaScript字符串 - 这可以是一个字符串,或者像在这种情况下一样,正则表达式 [ ^ ]。每次匹配正则表达式时,都会执行拆分。 \ W + 是这里的正则表达式: \ W 表示'非单词'字符, + 表示'1或更多'。因此,此拆分调用将拆分所有(系列)非单词字符。 + 是必要的,否则如果有人使用多个非单词字符,你就会在单词数组中得到空字符串。



这是\W的定义:


split splits a JavaScript string by a separator - this can be a string, or, like in this case, a regular expression[^]. Every time the regular expression gets matched, a split is performed. \W+ is the regular expression here: \W means 'non-word' character, + means '1 or more'. So this split call will split on all (series of) non-word characters. The + is necessary because otherwise you'd get empty strings in the word array if someone used multiple non-word characters next to each other.

This is the definition of \W:

匹配基本拉丁字母表中不是单词字符的任何字符。相当于[^ A-Za-z0-9_]。



例如,/ \ W /或/ [^ A-Za-z0-9 _] /匹配50%中的%。

Matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Za-z0-9_].

For example, /\W/ or /[^A-Za-z0-9_]/ matches "%" in "50%".


这篇关于如何将警报消息添加到文本最大的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆