Solved

first character in a string

Posted on 2006-06-12
4
1,076 Views
Last Modified: 2012-08-14
hi,
how do i find in javascript whether the first character in a string is a number,alphabet,or a special character?
0
Comment
Question by:jiniasp
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 50 total points
ID: 16884930
if(mystring.match(/^[a-z]/i)) {
  //alpha
} else if(mystring.match(/^\d/i)) {
  //numeric
} else {
  //other
}

-r-
0
 

Author Comment

by:jiniasp
ID: 16885188
Thanks Roonaan ur code worked.
If u don't mind, could u please explain the code above.
Can isNaN(x) function be used to find whether first character is number or not?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16885209
You can use isNaN(mystring.substring(0,1))

As to the above code. It uses regular expression:
/^[a-z]/i can be explained as:

^ - start of string
[a-z] - a single a-z character
/i - case insensitive search

\d is shorthand for [0-9], resulting in searching for a number at the start of a string: ^\d

-r-
0
 
LVL 3

Expert Comment

by:MrHorizontal
ID: 16885405
Put these special prototype functions anywhere in your javascript:

      String.prototype.IsAlpha= function()
      {
            return (this.match(/[A-Za-z]/g) ? true : false);
      }

      String.prototype.IsNumeric= function()
      {
            return (this.match(/[0-9]/g) ? true : false);
      }

      String.prototype.IsSpecial= function()
      {
            return (this.match(/[^A-Za-z0-9]/g) ? true : false);
      }

These mean that when you have a string or a number variable, you can simply call stringvar.IsAlpha() stringvar.IsSpecial() or stringvar.IsNumeric() which return true or false accordingly.

with your string, since you want the first character only do the following:

      var mystring = '0123AbCdEfG£$%^&';

      var isNumeric = (mystring+'').charAt(0).IsNumeric(); // check for number
      var isAlpha = (mystring+'').charAt(0).IsAlpha(); // check for alpha
      var isSpecial = (mystring+'').charAt(0).IsSpecial(); // check for alpha
      var isAlphaNumeric = (mystring+'').charAt(0).IsAlpha() || (mystring+'').charAt(0).IsNumeric(); // check for alpha

the only ones that will be true will be isNumeric and isAlphaNumeric, the others should be false.

you can also derive isSpecial by doing

      var isSpecial = !(mystring+'').charAt(0).IsAlpha() && !(mystring+'').charAt(0).IsNumeric();

Similarly, like Roonaan's layout, you can use the statements in an if or switch statement:

      if ((mystring+'').charAt(0).IsNumeric())
      {
            // numeric
      }
      else if ((mystring+'').charAt(0).IsAlpha())
      {
            // alpha
      }

Because these are prototypes of the base String object, this means you can use these as methods for *any* string, whether they are a variable, in an array or wherever.

In case you're wondering, I'm forcing (mystring+'') in the example above to force the variable into a string and test it, without changing the basic variable. If you try doing it with a number and you call the method, you'll get an exception.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now