22

I'm creating a browser-based form verification script that checks if the input doesn't have any uppercase characters according to Unicode Standards. My definition of an uppercase character is a character that has a lowercase mapping. If a certain character in the input string doesn't have a lowercase or uppercase mapping (like chinese characters) then it's alright (it should pass my validation).

I'm using UTF-8 encoding.

I'm planning to create a function that looks like this:

function hasUpper(str){
  if(str != str.toLowerCase()){
    return true
  }
  else {
    return false
  }
}

Will this work for my needs?

Thanks!

Albert
  • 3,611
  • 3
  • 28
  • 52

2 Answers2

11

Yes, that will work. Incidentally, the Boolean keywords are true and false, lower case. :)

Robusto
  • 31,447
  • 8
  • 56
  • 77
  • 1
    I the same vein the function can be shortened to: function hasUpper (str) { return str !== str.toLowerCase (); } Bah, no formatting in comments :-( – HBP Aug 28 '10 at 13:21
  • @Hans B PUFAL: asddYou can format by bracketing with the backwards accent character, which is the lowercase option on the tilde key (~). – Robusto Aug 28 '10 at 13:38
  • not neccesarily - im the # symbol is the lowercase option on my (UK) keyboard. The backtick symbol is above the tab key. – Nico Burns Aug 28 '10 at 14:45
  • @Nico Burns: Reminds me of Shaw's comment that England and America are two countries separated by a common language. Cheers. :) – Robusto Aug 28 '10 at 15:00
  • Thanks! the Boolean keywords are in lowercase, indeed. – Albert Aug 29 '10 at 22:40
3

The question answered here seems to indicate that toLower and toUpper are Unicode-aware:Stack OverFlow

And this one goes into it in more detail (indicating that some mappings conflict with the real world):more Stack Overflow

Hope this helps!

Community
  • 1
  • 1
A. Wilson
  • 8,534
  • 1
  • 26
  • 39