Reputation: 552
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>
Upvotes: 1
Views: 1825
Reputation: 4062
<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.
Upvotes: 1