SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression to Require Specific input rather than only allowing them.

    Hello my friends:
    I am trying to validate a password field, I notice there are many codes floating around the net, however I cannot see anytime which requires a specific input, they simply allow the user to enter specifics.
    I am thinking I should simply require a specific length and move on, thoughts on this?

    However, I want to try the Regex alternative in-case things change, unfortunately I know very little in this area.

    I simply need the field to required at least 1 letter, upper or lowercase, at least one number and any special character. So this would be valid: daftpunt@cool247

    Thanks for helping me.

    IC

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,049
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Try this:

    Code JavaScript:
    var password = "daftpunt@cool247";
    var valid  = password.match(/^(?=.*\d)(?=.*[A-Z])(?=.*[@#$%]).{6,20}$/i);
    if(valid){
      console.log("All good, brother!")
    } else {
      console.log("Didn't match the criteria!")
    }

    It checks for at least one digit, at least one upper or lower case letter, at least one of the special characters listed in the square brackets and it checks for a length of between 6 and twenty characters.

    Hope that helps.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi,

    Try this:

    Code JavaScript:
    var password = "daftpunt@cool247";
    var valid  = password.match(/^(?=.*\d)(?=.*[A-Z])(?=.*[@#$%]).{6,20}$/i);
    if(valid){
      console.log("All good, brother!")
    } else {
      console.log("Didn't match the criteria!")
    }

    It checks for at least one digit, at least one upper or lower case letter, at least one of the special characters listed in the square brackets and it checks for a length of between 6 and twenty characters.

    Hope that helps.
    Good morning my friend:

    Here is a problem I should have mentioned. I have C# Server Side Validation already in place using a Regex. This validation allows the following Characters, Example: daftpunk+=cool
    However, the Regex you provided works well but does not allow me to add certain characters. What I would really like to do is match the C# validation so that both JS and C# are validating the same entries.

    I am sorry for the confusion. just realized when I thoroughly tested; the server validation allows all of these: ~`@#$%^&*()-_+=\|{}[]"':;<>.,/?

    Thanks.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you already have a regex in the server side code then why not use the same regex in the JavaScript. Using a different one would be inconsistent.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    If you already have a regex in the server side code then why not use the same regex in the JavaScript. Using a different one would be inconsistent.
    Well, I tried doing that but I know very little about Regex and I had trouble with escaping certain areas.
    When it comes to certain complexities. Sometimes it's just easier to ask than trying to learn new technologies for everything on a page.
    I'll just leave it this way and let the C# validation catch whatever that comes through. It's minimum security and I am not worried about security.

    Thanks my friends:

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,049
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    A regex is a regex (at least as far as I know).
    If you don't mind sharing the one you already have, I will see if I can help you implement it in JS.

    Quote Originally Posted by Iconic_creator View Post
    It's minimum security and I am not worried about security.
    Famous last words

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Pullo View Post
    A regex is a regex (at least as far as I know).
    Not quite - there are actually two different categories of languages that support regex - one category uses a single pass to parse the data against the regex and the other category uses a double pass. The ones that use double pass actually support a couple of additional things that the single pass ones do not (such as lookbehind).

    I'd be really surprised though if any of the commands used in this regex were not supported in JavaScript (even though C# uses double pass and JavaScript only uses single).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Not quite - there are actually two different categories of languages that support regex - one category uses a single pass to parse the data against the regex and the other category uses a double pass. The ones that use double pass actually support a couple of additional things that the single pass ones do not (such as lookbehind).

    I'd be really surprised though if any of the commands used in this regex were not supported in JavaScript (even though C# uses double pass and JavaScript only uses single).
    Hello friends:

    I am using the Regex in an opposite fashion: Here is my Complete Validation Method is C#:

    It does the following:


    Field cannot be Null Or Empty
    Field must be greater than 8 and less than 16 (I can remove the less than 16 as the box is set to MaxLenght 16 in the properties panel, so this is not necessary).
    All 16 characters must be either letters or any number

    This is the tricky part at least for me: The password must not match the regex, meaning it can contain special characters. The entries are allowed if the regex is not match: By doing this: All characters are let through, except space/tab:

    Here is the code:

    Code C#:
            public static void PasswordInput(string checkPassword)
            {
                Validated.IsValidPassword = ((!string.IsNullOrEmpty(checkPassword)) &
              ((checkPassword.Length >= 8) & (checkPassword.Length <= 16)) &
               checkPassword.Take(16).Any(char.IsLetter) & checkPassword.Take(16).Any(char.IsDigit))
               & (!Regex.IsMatch(checkPassword, "^[a-zA-Z0-9]*$"));
            }

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    One JavaScript equivalent to (!Regex.IsMatch(checkPassword, "^[a-zA-Z0-9]*$")) would be: (!/^[a-zA-Z0-9]*$/.test(checkPassword))

    Alternatively you could use (!checkPassword.search(/^[a-zA-Z0-9]*$/))
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

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
  •