0

How to detect number, uppercase, lowercase data in string using JavaScript ?

Why does this code tell me $, # and ! are upper/lower case ?

How can I detect only number, uppercase, lowercase ?

https://jsfiddle.net/hqw12ov8/

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script> var strings = 'abCD12$#!'; var i=0; var character=''; while (i < strings.length){ character = strings.charAt(i); if (!isNaN(character * 1)){ alert('character is numric'); alert(strings.charAt(i)); }else{ if (character == character.toUpperCase()) { alert ('upper case true'); alert(strings.charAt(i)); } if (character == character.toLowerCase()){ alert ('lower case true'); alert(strings.charAt(i)); } } i++; } </script> 
1
  • what do you consider upper case Commented Jan 14, 2016 at 15:41

2 Answers 2

0

How can I test if a letter in a string is uppercase or lowercase using JavaScript?

This is where you have gotten the original code from correct? Here is a version that will remove the punctuation markers from this SO

How can I strip all punctuation from a string in JavaScript using regex?

var strings = 'this iS a TeSt 523 Now!'; var punctuationless = strings.replace(/[.,\-\/#!$%\^&\*;:{}=\-_`~()]/g,""); var finalString = punctuationless.replace(/\s{2,}/g," "); var i=0; var character=''; while (i <= strings.length){ character = strings.charAt(i); if (!isNaN(character * 1)){ alert('character is numeric ' + character); }else{ if (character == character.toUpperCase()) { alert ('upper case true ' + character); } if (character == character.toLowerCase()){ alert ('lower case true ' + character); } } i++; }

I don't know if you want to include this punctuation in your check, since punctuation is neither upper or lowercase.

Sign up to request clarification or add additional context in comments.

Comments

0

In JavaScript toUpperCase() works only with lower case alphabets. Other upper case letters and non-alphabetic characters (like A, B, Y, Z, $, _ etc.) remains unchanged. So '$'.toUpperCase() returns '$'. The same happens with toLowerCase(). So when character is '$' or '#', character==character.toUpperCase() returns true.

The following code should do what you want. Here I am checking the Unicode value of each character with String.prototype.charCodeAt(index).

var strings='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789($_)'; for(var i=0;i<strings.length;i++) { var c=strings.charCodeAt(i); var s=strings.charAt(i); if(c>=48&&c<=57) //if(s.match(/[0-9]/)) alert(s+' is Number'); else if(c>=97&&c<=122) //else if(s.match(/[a-z]/)) alert(s+' is Lower Case'); else if(c>=65&&c<=90) //else if(s.match(/[A-Z]/)) alert(s+' is Upper Case'); else alert(s+' is neither Number or Alphabet'); } 

Use the lines in the comments instead of the lines before them for a simple RegExp support. It will also help to remove var c=strings.charCodeAt(i);.

2 Comments

Using these character code ranges seems a lot less intuitive than using a regexp.
I could have used RegExp. This code is just easy to understand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.