JavaScript
  Home arrow JavaScript arrow Page 5 - Stringing Things Along
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Stringing Things Along
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2003-01-22

    Table of Contents:
  • Stringing Things Along
  • Elementary School
  • When Size Does Matter
  • Slice And Dice
  • Building Character
  • Search And Destroy
  • Bigger, Bolder, Better

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Stringing Things Along - Building Character
    (Page 5 of 7 )

    You can use the indexOf() method to locate the first occurrence of a particular set of characters in a string
    <script language="Javascript">
    // set string
    var str = "It's a bird! It's a plane! It's Superman!";
    // returns 5
    alert(str.indexOf("a"));
    </script>
    and the lastIndexOf() method to locate its last occurrence.
    <script language="Javascript">
    // set string
    var str = "It's a bird! It's a plane! It's Superman!";
    // returns 38
    alert(str.lastIndexOf("a"));
    </script>
    Both these functions return the position of the substring in the original string, or -1 if no match is found.

    Of course, the indexOf() method only returns the first match - but what if you need to find the second or third match? Simply add an argument to the method call, indicating where in the string the indexOf() method should begin searching:
    <script language="JavaScript">
    // set string
    var str = "It's a bird! It's a plane! It's Superman!";
    // returns 18
    alert(str.indexOf("a", 10));
    </script>
    You can use the charAt() method to obtain the character at a particular index position within the string.
    <script language="JavaScript">
    // set string
    var str = "Batman and Robin";
    // returns "t"
    alert(str.charAt(2)); 
    </script>
    A common application of the charAt() method is to iterate through a string, checking each character for validity against a pre-defined list. Consider the following example, which demonstrates how this works in the context of a password validator:
    <html>
    <head>
    <script language="JavaScript">
    // verify password string against allowed list of characters function
    checkPassword() {
    // error flag
    var error = 0;
    // list of valid characters
    var validChars = "abcdefghijklmnopqrstuvwxyz1234567890";
    // get value from form
    var password = document.forms[0].password.value;
    // iterate through string and check against allowed character
    list
    for (x=0; x<password.length; x++)
    {
    c = password.charAt(x);
    if (validChars.indexOf(c) == -1)
    {
    error = 1;
    break;
    }
    }
    // display appropriate message
    if (error == 1)
    {
    alert ("Error!");
    return false;
    }
    else
    {
    alert ("All OK!");
    return true;
    }
    }
    </script>
    </head>
    <body>
    <form>
    Enter password: <input name="password" type="text">
    <br>
    <input type="submit" value="Check!" onClick="checkPassword()"> </form>
    </body>
    </html>

    More JavaScript Articles
    More By Harish Kamath, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

    Iron Speed
     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway