www.webdeveloper.com
Results 1 to 3 of 3

Thread: regular expression newbie

  1. #1
    Join Date
    Jan 2013
    Posts
    1

    regular expression newbie

    I will be the first to admit that I do not know much about regular expression, in javascript or anywhere else. Here's what I need to do.
    I am verifying a supplied password to make sure it contains

    at least one capital letter
    at least one numeric
    at least one special character (!@#$%^&*+)
    is at least 8 characters

    Here's the function I'm using currently.

    var password = $('#password').val();
    var re = new RegExp("!@#$%^&*");
    if(!re.test(password))
    alert("No special characters");
    re = new RegExp("0123456789");
    if(!re.test(password))
    alert("No numbers");

    This is just test code so please don't comment on how bad it is, I'm trying to get it to work before I make it pretty.

    Right now, no matter what I type the code is saying that the password contains no numbers and no special characters.
    Clearly I'm doing something wrong, but what is it?
    Can anyone point me in the right direction? Thanks.

  2. #2
    Join Date
    Jan 2013
    Posts
    1

    try this in firebug

    var password = $('#password');
    //will check word disgit and mention special character in the password
    var re = new RegExp("[!@#\$\%\^&\*\w\d]*");
    if(!re.test(password))
    alert("No special characters");
    re = new RegExp("\d*");
    if(!re.test(password))
    alert("No numbers");

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    A set of characters must be specified within []

    The RegExp constructor is not needed for this exercise but I have retained it:

    Code:
    <script type="text/javascript">
    
    var password = '@2';
    var re = new RegExp("[!@#$%^&*]");
    if(!re.test(password))
     alert("No special characters");
     
    re = new RegExp("[0123456789]");
    if(!re.test(password))
     alert("No digits");
    
    </script>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

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