1

i want to write a code to check if the text inside the textarea is ascii or not , if it's ascii set the maximaum length to 160 if it's not set the maxiamum lenghh to 70 , also i want the check to be while the user typing the text , i tried this but not work,,, any idea?

<script type="text/javascript">
var maxLength=160;
var Ascii=true;


 function isAscii(el) {
     var i=0;
 while ( i < = el.value.length ){
 if(el.value[i].charCodeAt(0) >= 0 && el.value[i]charCodeAt(0) <= 127 ){
 i=i+1;
 } 
 else 
 {
 return false
} 
}
return true;
  }



  function characterCount(el) {

if ( isAscii(el)){
Ascii=true;
maxLength=160;
}
else {
Ascii=false
maxLength=70;
}




var charCount = document.getElementById('charCount');
if (el.value.length > maxLength) el.value = el.value.substring(0,maxLength);
if (charCount) charCount.innerHTML = maxLength - el.value.length;
return true;
 }


</script>

  <textarea name='text' onKeyUp='characterCount(this)' id='textarea' cols='60'       rows='10'> </textarea>
Omar
  • 542
  • 1
  • 6
  • 27
  • The *encoding* will not change from ASCII to UTF-8 while the user types. You simply want to check whether the user has entered a character outside the ASCII range of characters. – deceze Jul 03 '13 at 08:01
  • yes this is what i want to do ,,, i've changed the question title – Omar Jul 03 '13 at 08:04

1 Answers1

1
    <script type="text/javascript">
var maxLength=160;
var Ascii=true;


 function isAscii(el) {
     var i=0;
 while ( i < el.value.length ){
 alert(el.value[i]);
 if(el.value[i].charCodeAt(0) >= 0 && el.value[i].charCodeAt(0) <= 127 ){
 i=i+1;
 } 
 else 
 {
 return false
} 
}
return true;
  }



  function characterCount(el) {

if ( isAscii(el)){
Ascii=true;
maxLength=160;
}
else {
alert("non-ascii");
Ascii=false
maxLength=70;
}





return true;
 }


</script>
<body>
  <textarea name='text' onKeyUp='characterCount(this)' id='textarea' cols='60' > </textarea>
 </body>

above code alerts for ascii non-ascii characters, I found no point in changing the length of text box on length, but anyways hope you can do that now.

changes in code:

1) el.value[i].charCodeAt(0) -> .(dot) is missing

2) while ( i < el.value.length ){

in above statement you have used <= which was again wrong.

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40