Monday, January 28, 2013

How to check a comment/forum post for capitalization in javascript in Greek.


The function bellow works with the greek alphabet. If you wanted to do the same for latin you could use the isUpperCase function.
 function checkcapital (str) {
  var capitals = new Array('Ά','Α','Β','Γ','Δ','Ε','Έ','Ζ','Η','Ή','Θ','Ι','Ί','Κ','Λ','Μ','Ν','Ξ','Ο','Ό','Π','Ρ','Σ','Τ','Υ','Ύ','Φ','Χ','Ψ','Ω','Ώ');
  var lowers = new Array('α','ά','β','γ','δ','ε','έ','ζ','ή','η','θ','ι','ί','κ','λ','μ','ν','ξ','ό','ο','π','ρ','σ','τ','ύ','υ','φ','χ','ψ','ώ','ω','ς');
  var nr_capitals = 0;
  var nr_lowers = 0;
  var i = str.length;
  var allow = 1;
  
  if (i>10) {  
   while (i--) {
    if (capitals.indexOf(str[i]) >= 0) {
     nr_capitals++;
    }
    if (lowers.indexOf(str[i]) >= 0) {
     nr_lowers++;
    }
   }
   
   if (nr_capitals>nr_lowers) {
    allow = 0;
   }
  }
  return allow;
 }