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.



Try this:

var password = "daftpunt@cool247";
var valid  = password.match(/^(?=.*\\d)(?=.*[A-Z])(?=.*[@#$%]).{6,20}$/i);
  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: ~`@#$%^&*()-_+=\|{}[]"':;<>.,/?


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:

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.

Famous last words :slight_smile:

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:

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, “[1]*$”));

  1. a-zA-Z0-9 ↩︎

One JavaScript equivalent to (!Regex.IsMatch(checkPassword, “[1]*$”)) would be: b[/b]

Alternatively you could use b[/b]

  1. a-zA-Z0-9 ↩︎