原型问题 - 如何检查输入字段上的链接并检查该输入字段的长度?

时间:2010-01-10 02:40:11

标签: javascript prototypejs

我开始为此编码了。它几乎正常工作。 我的目标: 1)检查字段中输入的长度或网址(总长度),如果长度大于20,则将每个链接的长度减少20 2)确定输入字段中剩下的字符

profile.js中的javascript(原型):

function checkurl_total_length(text) {
    var text = "";
    var matches = [];
    var total_length = 0;
    var urlRegex =  /(http|https):\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;

    text.scan(urlRegex, function(match){ matches.push(match[0])});
    for (var index = 0; index < matches.length; ++index) {
        item = matches[index];
        reduce_length = matches.length*20;
        if(item.length>20) {
            total_length = total_length + item.length - reduce_length;
        }
        else {
            total_length = total_length + item.length;
        }


    }

    return total_length;
}

function count_characters(field){
   var limitNum=140;
    var link_length = 0;
   if(checkurl_total_length(field.value)!=0) {
      link_length =link_length+ checkurl_total_length(field.value);
    }
    else {
      link_length = 0;
   }
     limitNum = limitNum+link_length;

    if( link_length !=0 ){
     $("links").update("with links");

    }
    left = limitNum-field.value.length;

    $("count").update(left);

}

HTML

<!DOCTYPE HTML>
<html lang="en"><head>


<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>JUST A TEST FILE</title>
<script src="prototype.js" type="text/javascript"></script>
<script src="profile.js" type="text/javascript"></script>

</head><body>
<h1>
CHARACTERS COUNT
</h1>
<div class="container_24">
    <h2 id="title2">
        TESTING
    </h2>
    <div class="grid_24">

        <div id="count"></div>
         <br /s>

        <div id="links"></div>
   <form >

    <textarea wrap="hard" onpaste="count_characters(this);" onkeyup="count_characters(this);" onkeydown="count_characters(this);"  id="updates" onfocus="count_characters(this);" name="test"/> </textarea>
    <input type="submit" value=" " name="commit" disabled=""/>
    </form>
    </div>

</div>
<!-- end .container_24 -->
</body></html>

计算剩下的字符是有效的,但检查网址和网址的长度不是。为什么这不起作用的任何提示?

1 个答案:

答案 0 :(得分:0)

不确定,但这应该是

checkurl_total_length(field.value!=0)   // ) != 0