Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2004
    Location
    Hershey, PA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expression Sanity Check

    I'm new to regular expressions and want to validate an inputed username that must be 3 to 10 alphanumeric characters. I've tested the listing below and it seems pretty solid. Are there any experienced folks out there that care to give it a blessing or comment?

    var entry = /^([a-zA-Z0-9]{3,10})$/;
    var strUserName = document.forms.frmLogin.txtUserName.value;
    if (!entry.test(strUserName)) {
    ... show error msg, ...

    PS: I know the forum already has threads on this topic, but they were all a bit too complicated for my pea-sized brain.

  • #2
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    err... if it works, what's the problem?

    Only things I notice are that the parentheses aren't necessary unless you plan on using the contents of the sub-pattern match (which is identical to the full text in this case, assuming the match works) and also that most sites allow usernames with underscores. I'm not great with regexps and I'm even more rusty with JavaScript, so correct me if I'm wrong.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    It has my blessing...
    But, you could do it in a much more efficient way than that (allows for underscore, though, which you might not want):
    Code:
    /^\w{3,10}$/
    or without underscore
    Code:
    /^[a-z0-9]{3,10}$/i
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New Coder
    Join Date
    Jun 2004
    Location
    Hershey, PA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both for the comments and shortcuts. pt


  •  

    Posting Permissions

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