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

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
JAVASCRIPT

Stringing Things Along
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

     
     
    ADVERTISEMENT


    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

    - Introduction to JavaScript
    - Adding Elements to a Tree with TreeView jQue...
    - Using the Persist Argument in a TreeView jQu...
    - Using Unique and Toggle in a TreeView jQuery...
    - Using Event Delegation for Mouseover Events ...
    - Using the Animate Option in a Treeview jQuer...
    - Using HTML Lists with Event Delegation in Ja...
    - Opened and Closed Branches on a TreeView jQu...
    - Mouseover Events and Event Delegation in Jav...
    - Creating a TreeView JQuery Hierarchical Navi...
    - Event Delegation in JavaScript
    - A Look at the New YUI Carousel Control
    - Working with Draggable Elements and Transpar...
    - Displaying Pinned Handles with Resizable Con...
    - Building Resizable Containers with the Ext J...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek