1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
<html>
<head>
<meta name="author" content="Aurelie DAVID">
</meta>
<script langage="JavaScript">
//method used in webcor-detail.xhtml
//it analyses text wrotten in textarea and detect if
//number max of line is exceeded and return a pop-up which alert user.
//param text string value of textarea
//return new string value of textarea
function formatTextarea(text)
{ var overLimits=false; //boolean initialized at false, which fixes if the text wrotten exceeds limits or not
var nbLinesMax=7; //number max of lines in textarea
var widthMax=21; //width max of textarea
var textTemp=text; //temporary text will be truncated in analyze
var tabTextarea=new Array(); //init an array which will be contains the new text truncated
var k=0; //init the index of character of text wrotten which will be analized in loop
for(var i=0;i<nbLinesMax;i++){
for(var j=0;j<=widthMax;j++){
if(textTemp.length==0){ //if there is no text to analyze, stop analyze
j=widthMax;
i=nbLinesMax;
}else if(k==textTemp.length-1){ //if it's the last character of text, register the rest of text and stop analyze
tabTextarea[i]=textTemp;
textTemp="";
j=widthMax;
i=nbLinesMax;
}else if(textTemp.charAt(k)=='\n'){ //if the character analyzed is a breaking line
tabTextarea[i]=textTemp.substring(0,k+1);
textTemp=textTemp.substring(k+1);
k=0;
j=widthMax;
}else if(j==widthMax && textTemp.charAt(k)==' '){ //if it's the end of line and the character analyzed is a space
tabTextarea[i]=textTemp.substring(0,k+1);
textTemp=textTemp.substring(k+1);
k=0;
}else if(j==widthMax && textTemp.charAt(k+1)==' '){ //if it's the end of line and the character after the character analyzed is a space
tabTextarea[i]=textTemp.substring(0,k+1);
textTemp=textTemp.substring(k+1);
k=0;
}
else if(j==widthMax && textTemp.charAt(k)!=' ' && textTemp.charAt(k+1)!=' '){ //if it's the end of line and the character analyzed is a character in a middle of a word
while(textTemp.charAt(k)!=' '){
k--;
if(k==0){
k=widthMax;
textTemp=textTemp.substring(0,k)+"\n"+textTemp.substring(k);
overLimits=true;
break;
}
}
tabTextarea[i]=textTemp.substring(0,k+1);
textTemp=textTemp.substring(k+1);
k=0;
}else{
k++;
}
}
}
if(textTemp.length>0){ // if the text has other lines, the flag overLimits is fixed at true
overLimits=true;
}
textTemp="";
for(var l=0;l<tabTextarea.length;l++){
textTemp=textTemp+tabTextarea[l];
}
if(overLimits){
text=textTemp;
alert("Attention vous avez dépassé le nombre de caractères ou de lignes autorisés, votre texte sera tronqué");
}
return text;
}
</script>
</head>
<body>
<TEXTAREA style="font-family:Courier New; font-size:8pt" rows="7" cols="20" name="commentaires" OnChange="this.value=formatTextarea(this.value);">
</TEXTAREA>
</body>
</html> |
Partager