SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Post Using indexOf to search for multiple characters

    I'd like to add a javascript validation to my password form to ensure that users are using special characters. I validate server side as well, but I'd like to add this on top of things.

    So far I've found that using indexOf and looking for -1 if the specific character does not exist will allow me to alert the user if they have not used a specific special character. For instance:

    Code:
    if (Password.indexOf("@") == -1)
      {alert("The passwords you have selected do not contain the @ symbol.");
      myForm.NewPassword.focus();
      myForm.NewPassword.select();
      myForm.buSubmit.disabled = false;
      myForm.buReset.disabled = false;
      return false}
    However, what if I wanted to search to make sure that the string "Password" contained EITHER the @ symbol OR the # symbol? Either one would qualify the password and should return true, but the lack of either symbol should return false. I'm not sure what the correct syntax for ths OR feature would be.

    Thanks!
    TAKE A WALK OUTSIDE YOUR MIND.

  2. #2
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I've made things work, but I'm sure that this can be cleaned up. It seems a bit crufty. Any advice?

    var SpecCount;
    SpecCount = 0;
    if (Password.indexOf("!") > -1) {SpecCount ++;}
    if (Password.indexOf("@") > -1) {SpecCount ++;}
    if (Password.indexOf("#") > -1) {SpecCount ++;}
    if (Password.indexOf("$") > -1) {SpecCount ++;}
    if (Password.indexOf("%") > -1) {SpecCount ++;}
    if (Password.indexOf("^") > -1) {SpecCount ++;}
    if (Password.indexOf("&") > -1) {SpecCount ++;}
    if (Password.indexOf("*") > -1) {SpecCount ++;}
    if (Password.indexOf("(") > -1) {SpecCount ++;}
    if (Password.indexOf(")") > -1) {SpecCount ++;}
    if (Password.indexOf("~") > -1) {SpecCount ++;}
    if (Password.indexOf("`") > -1) {SpecCount ++;}
    if (Password.indexOf("?") > -1) {SpecCount ++;}
    if (SpecCount < 1)
    {alert("The passwords you have selected do not contain any special characters.");
    myForm.NewPassword.focus();
    myForm.NewPassword.select();
    myForm.buSubmit.disabled = false;
    myForm.buReset.disabled = false;
    return false}
    TAKE A WALK OUTSIDE YOUR MIND.

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    Code:
    var SpecCount = 0;
    var symbols = "!@#$&#37;^&*()~`?";
    for(var i=0,l=symbols.length;i<l;i++){
      if(Password.indexOf(symbols.charAt(i)) > -1) SpecCount++;
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    That doesn't seem to be enforcing special characters. I've beefed up my code since my last post, so I've included the updated code here:

    Code:
    // Password Complexity
    Code:
    var Symbols = "!@#$%^&*()~`?";
    var SpecCount = 0;
    var NumCount = 0;
    // Counts Number of Upper Case Letters
    var UpperCount = Password.replace(/[^A-Z]/g, "").length;
    // Counts Number of Lower Case Letters
    var LowerCount = Password.replace(/[^a-z]/g, "").length;
    // Checks for Special Characters in Password String
    for(var i=0,l=Symbols.length;i<l;i++){
      if(Password.indexOf(Symbols.charAt(i)) > -1) SpecCount ++;
    }
    // Checks for Numbers in Password String
    if (Password.indexOf("1") > -1) {NumCount ++;}
    if (Password.indexOf("2") > -1) {NumCount ++;}
    if (Password.indexOf("3") > -1) {NumCount ++;}
    if (Password.indexOf("4") > -1) {NumCount ++;}
    if (Password.indexOf("5") > -1) {NumCount ++;}
    if (Password.indexOf("6") > -1) {NumCount ++;}
    if (Password.indexOf("7") > -1) {NumCount ++;}
    if (Password.indexOf("8") > -1) {NumCount ++;}
    if (Password.indexOf("9") > -1) {NumCount ++;}
    if (Password.indexOf("0") > -1) {NumCount ++;}
    if (SpecCount < 1)
     {
     alert(Password.replace(/[^A-Z]/g, "").length);
     alert("The passwords you have selected do not contain any special characters.");
     myForm.NewPassword.focus();
     myForm.NewPassword.select();
     myForm.buSubmit.disabled = false;
     myForm.buReset.disabled = false;
     return false}
    if (NumCount < 1)
     {alert("The passwords you have selected do not contain any numbers.");
     myForm.NewPassword.focus();
     myForm.NewPassword.select();
     myForm.buSubmit.disabled = false;
     myForm.buReset.disabled = false;
     return false}
    if (UpperCount < 1)
     {alert("The passwords you have selected do not contain any upper case letters.");
     myForm.NewPassword.focus();
     myForm.NewPassword.select();
     myForm.buSubmit.disabled = false;
     myForm.buReset.disabled = false;
     return false}
    if (LowerCount < 1)
     {alert("The passwords you have selected do not contain any lower case letters.");
     myForm.NewPassword.focus();
     myForm.NewPassword.select();
     myForm.buSubmit.disabled = false;
     myForm.buReset.disabled = false;
     return false}
    }
    TAKE A WALK OUTSIDE YOUR MIND.

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry. Apparently it wasn't as obvious as I thought.

    The code I posted is the equivalent of all but the last conditional in your second post. You need to put that conditional after the code I posted for it to work.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    GOT IT!

    Thanks.
    TAKE A WALK OUTSIDE YOUR MIND.


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
  •