2010-01-10 96 views
1

我開始爲此編碼。它幾乎工作。 我的目標: 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); 

} 

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> 

計數剩餘字符是工作,但檢查URL和URL的長度是沒有的。任何提示爲什麼這不起作用?

+0

我更新了基於下面的評論的代碼。我仍然沒有解決這個問題。 – kgpdeveloper 2010-01-10 03:04:54

+0

好吧,根據我的測試,正則表達式是錯誤的:var urlRegex = /(http|https):\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z ] {3} /;似乎只承認幾個網址。任何幫助正則表達式? – kgpdeveloper 2010-01-10 04:32:16

+0

這似乎工作:/(ftp | http | https):\/\ /(\ w +:{0,1} \ w * @)?(\ S +)(:[0-9] +)?(\ /|\/([\w#!:.?+=&%@!\-\/])?? – kgpdeveloper 2010-01-10 04:36:16

回答

0

不知道,但是這應該是

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

感謝您的注意,但它仍然是錯誤的。我更新了代碼。 – kgpdeveloper 2010-01-10 03:03:17