开发者

Prototype issue - How do I check for links on an input field and check for the length of that input field?

I sort of started coding for this. It's almost working. My goals: 1) Check for the length or url's entered in a field (the total length) and reduce each link's length by 20 if the length is greater than 20 2) Determine the characters left in an input field

The javascript in profile.js (prototype):

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_le开发者_Go百科ngth + 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);

}

THE 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>

Counting characters left is working but checking for url and the length of the url isn't. Any hints on why this isn't working?


not sure, but should this be

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜