SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var checkStr = theForm.Alias.value;
    var allValid = true;
    for (i = 0;  i < checkStr.length;  i++) {
    	ch = checkStr.charAt(i);
    	for (j = 0;  j < checkOK.length;  j++)
    	if (ch == checkOK.charAt(j))
    	break;
    	if (j == checkOK.length) {
    		allValid = false;
    		break;
    	}
    }
    
    if (!allValid) {
    	alert("Please enter only letter and numeric characters in the \"Alias\" field.");
    	theForm.Alias.focus();
    	return (false);
    }
    Can this be modified to test for a specific order of characters/digits?

    I'd like to test for format: A1234567
    - First character is alpha (A-Z, a-z)
    - Next 7 characters are digits (0-9)

    I can do this in PHP, but I'd like to know how to make it work in Javascript.

    thanks!

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'd do it the same way as I would in PHP -- with regular expressions. Check out my article on WebmasterBase.com, Regular Expressions in JavaScript for a quick how-to. The method should be pretty obvious after reading the article, but post back if you have any trouble!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •