dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: validate for numbers and letters

  1. #1
    Join Date
    Feb 2005
    Posts
    1

    validate for numbers and letters

    I'm fairly new to Javascript and need a little help. How do I validate a field to ensure it contains at least one alpha character and at least one numeric character? Any help is appreciated.

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    <input onchange="if (!/[a-z]/i.test (this.value) || !/\d/.test (this.value)) {alert (); this.focus()}" type="text">
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Oct 2004
    Posts
    1,118
    The easiest way is to use regular expressions.

    tutorial:
    http://www.sitepoint.com/article/expressions-javascript

    list of symbols:
    http://www.siteexperts.com/tips/func...ts23/page1.asp

    Regular expressions have a test() function, which returns true or false depending on whether a given match was found in a given string. For instance, here is a simple regex:

    var my_regex = /[a-z]/;

    That expression will match any lower case letter in a string. So, you could do this:

    var my_regex = /[a-z]/;
    var result = my_regex.test("123456a");

    That bit on the right of the equals sign says to search the string "123456a" for the pattern stored in the variable my_regex. Since the symbols stored in the variable my_regex will match any lower case letter, if any lower case letter is found in the string, then result will be true, otherwise result will be false.
    Last edited by 7stud; 02-01-2005 at 08:23 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles