Results 1 to 3 of 3

Thread: Validation to allow numbers letters only (Regular Expression)

  1. #1
    Join Date
    Feb 2009
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Validation to allow numbers letters only (Regular Expression)

    function usernameValidate(usernameString) {
    var valid = true;

    if ( usernameString == "" ) {
    feedback('userMess','Enter your username here',7);
    valid = false;
    }
    else if ( usernameString.length <= 5 ){
    feedback('userMess','Username too short',7);
    valid = false;
    }
    else if ( !validUsernameString(usernameString) ) {
    feedback('userMess','Username contails illegal characters',7);
    valid = false;
    }
    else feedback('userMess', '✔',1);

    if ( valid ) return true;
    else return false;
    }


    function validUsernameString(usernameString) {
    // regular expression
    var filter = /^[a-z]|[0-9]/;
    if (filter.test(usernameString)) return true;
    else return false;
    }

    Im pretty sure the regular expression used is incorrect. The field only requires the text box to allow only numbers and letters and no other characters. At the moment you are able to enter any character in the textbox and it says it is valid.

    What would be the correct regular expression to use for this type of validation?

    Regards
    Akin

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    /^[A-Za-z0-9]+$/
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2009
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Cheers mate works now

    Much appreciated
    Akin

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
  •