alphanumeric space validation

Discussion in 'Javascript' started by pradeep, Mar 5, 2007.

  1. pradeep

    pradeep Guest

    Hi

    How i check alphnumeric & space validation for input text ?
    e.g.
    input : "abc GNM 2" is valid &
    input : "abc GNM %2" is invalid

    guide me.
    pradeep, Mar 5, 2007
    #1
    1. Advertising

  2. Re: alphanumeric space validation

    pradeep wrote:

    > How i check alphnumeric & space validation for input text ?
    > e.g.
    > input : "abc GNM 2" is valid &
    > input : "abc GNM %2" is invalid


    var str = '4uP m'
    if (/^[a-z\s\d]*$/i.test(str)) {
    alert('OK')
    } else {
    alert('not OK')
    }

    Hope this helps,

    --
    Bart
    Bart Van der Donck, Mar 5, 2007
    #2
    1. Advertising

  3. pradeep

    Yanick Guest

    Re: alphanumeric space validation

    On Mar 5, 4:31 am, "pradeep" <> wrote:
    > Hi
    >
    > How i check alphnumeric & space validation for input text ?
    > e.g.
    > input : "abc GNM 2" is valid &
    > input : "abc GNM %2" is invalid
    >
    > guide me.


    Bart is right, using regular expressions is the way to go, but if you
    don't really understand them (yet), here's a describing example of
    your solution :


    /**
    * The function receives a string as first
    * parameter and test if it only contains
    * letters, numbers and spaces, and returns
    * true. If any other characters are found,
    * it returns false
    */
    function alphaNumericValidator(str) {
    // if empty fields are invalid,
    // replace the * by a +
    // * = if any or more
    // + = 1 or more
    // \w = a letter (= [a-zA-Z])
    // \d = a number (= [0-9])
    // \s = white space (= [ \n\r\t])
    // \n = new line
    // \r = carriage return
    // \t = tabs
    // ^ = the beginning of the string
    // $ = the end of the string
    //
    // [\w\d\s] = [a-zA-Z0-9 \n\r\t]
    return /^[\w\d\s]*$/.test(str);
    }


    /**
    * Test case
    */

    // strings to test; these values
    var test1 = 'abc GNM 2';
    var test2 = 'abc GNM %2';

    if ( alphaNumericValidator(test1) ) {
    alert( "test1 is valid" );
    } else {
    alert( "test1 is invalid" );
    }

    if ( alphaNumericValidator(test2) ) {
    alert( "test2 is valid" );
    } else {
    alert( "test2 is invalid" );
    }


    -yanick
    Yanick, Mar 6, 2007
    #3
  4. Re: alphanumeric space validation

    Yanick wrote:

    > On Mar 5, 4:31 am, "pradeep" <> wrote:
    >
    > > Hi

    >
    > > How i check alphnumeric & space validation for input text ?
    > > e.g.
    > > input : "abc GNM 2" is valid &
    > > input : "abc GNM %2" is invalid

    >
    > > guide me.

    >
    > Bart is right, using regular expressions is the way to go, but
    > if you don't really understand them (yet), here's a describing
    > example of your solution :
    >
    > [...]
    > return /^[\w\d\s]*$/.test(str);
    > [...]


    \w allows underscore.

    --
    Bart
    Bart Van der Donck, Mar 6, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,884
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,649
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,006
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    717
    Michael Hudson
    Oct 24, 2003
  5. Avnish
    Replies:
    1
    Views:
    249
    Martin Honnen
    Jul 5, 2003
Loading...

Share This Page