Jump to content
Sign in to follow this  
Raktim

RegExp (Password Validation)

Recommended Posts

I try a RegExp for validation which Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters.

<input id="name" pattern="(?=.*\d)(?=.*[A-z])(?=.*[a-z]).{8,}"> 

Now I want to modify it which input must contain two number instead of one number. I tried that like...

<input id="name" pattern="(?=.*\d{2})(?=.*[A-z])(?=.*[a-z]).{8,}"> 

It works fine if I input two numeric digits Consecutive. Mean if I input 87aBcdfgf, then it is right. But 8aBcd7dfg input is wrong.

Please, modify my RegExp. which can take input 8aBcd7dfg like that.

Share this post


Link to post
Share on other sites

Yes Sir, But in this way, if I want n digits then it needs to right n patterns! Sir, is it possible to do it in any other way?

Edited by Raktim

Share this post


Link to post
Share on other sites

Not that I can think of, in fact it won't even work to add more than one pattern when you're using positive lookahead, it will still only require 1 digit.  You would need to test it with multiple expressions.

Share this post


Link to post
Share on other sites
4 hours ago, Raktim said:

okay

Would something like this work?

(?=(.*\d){2,})(?=.*[A-Z])(?=.*[a-z]).{8,}

Test data:

aaBcde7fg - No Match (1 number)
aaBcd67fg - Match
87aBcdfgf - Match
8aBcd7dfg - Match

  • Thanks 1

Share this post


Link to post
Share on other sites
18 hours ago, Funce said:

Would something like this work?


(?=(.*\d){2,})(?=.*[A-Z])(?=.*[a-z]).{8,}

Test data:

aaBcde7fg - No Match (1 number)
aaBcd67fg - Match
87aBcdfgf - Match
8aBcd7dfg - Match

Yes, It perfectly works.

 

 

Share this post


Link to post
Share on other sites

Sir, I want to make another regex that accepts any character at least 8 characters long except (0-9). How to write that?

I try it like...

[A-z!"#$%&'()*+,-.\/:;<=>?@[\]^_`{|}~]{8,}

Can I write this same regexp in another way?

Edited by Raktim

Share this post


Link to post
Share on other sites

Okay.

I already use the following pattern which takes At least two number, one lowercase letter one uppercase letter and at least 8 characters.

(?=(.*\d){2})(?=.*[A-z])(?=.*[a-z]).{8,}

Means, 4 characters (2 number + 1 lowercase + 1 uppercase letter)  are must need and other characters can be anything .

Now, I want that other charecter can be anyhthing except number. I replace * with [^0-9].

(?=(.*\d){2})(?=.*[A-Z])(?=.*[a-z])[^0-9]{8,}

But, it does't work. Why?

Edited by Raktim

Share this post


Link to post
Share on other sites

I also have another question

(?=(.*\d){2})(?=.*[A-Z])(?=.*[a-z])

If I use this pattern then I think it takes a total of 4 characters ( 2 number + 1 lowercase + 1 uppercase letter ) to match.

Tested Data:

Ab45 ->Not Match

a4F8-> Not Match

What actually happens? What is the meaning of this pattern?

Share this post


Link to post
Share on other sites

But, it does't work. Why?

You're telling it that you both need 2 digits and also that there can't be any digits.  

 

For your other pattern, I do get matches for those 2 strings.

Share this post


Link to post
Share on other sites
13 hours ago, justsomeguy said:

For your other pattern, I do get matches for those 2 strings.

 

But, those 2 string does not full match.

1 string only charecter 5 is matched and 2nd string only F8 are matched. But I want to do match the complete string. How to modify that?

 

Tested string.JPG

Edited by Raktim

Share this post


Link to post
Share on other sites

I want to make that input take only 2 number at least one lowercase, one uppercase and at least 8 characters except number.

I tried this pattern...

(?=(.*\d){2})(?=.*[A-z])(?=.*[a-z])[A-z!"#$%&'()*+,-.\/:;<=>?@[\]^_`{|}~]{8,}

But it totally fails. Then what should be my correct pattern to do that?

I want Like...

Abc4k9h2L ->FALSE

Abc4k9hL+ ->TRUE

Abc4l@s$kl4 ->TRUE

Edited by Raktim

Share this post


Link to post
Share on other sites

I'm not sure how to do it with one pattern, I don't use lookahead or lookbehind very often.  I would use multiple patterns, but there's probably a way to do it with one.

  • Like 1

Share this post


Link to post
Share on other sites

Sir, I make two pattern 

[A-z]{7}\d

which can match this string Raktimf8

If I change the position of \d like

\d[A-z]{7}

then it can match this string 8Raktims

How I match this pattern Rakti8ms

Share this post


Link to post
Share on other sites
On 9/13/2019 at 8:03 PM, Raktim said:

But, those 2 string does not full match.

1 string only charecter 5 is matched and 2nd string only F8 are matched. But I want to do match the complete string. How to modify that?

You've gotta modify this regex so you're using the entire string that matches as follows.

(?=(.*\d){2})(?=.*[A-Z])(?=.*[a-z]).*

Does this work for this one?

 

On 9/13/2019 at 9:40 PM, Raktim said:

I want to make that input take only 2 number at least one lowercase, one uppercase and at least 8 characters except number.

I tried this pattern...


(?=(.*\d){2})(?=.*[A-z])(?=.*[a-z])[A-z!"#$%&'()*+,-.\/:;<=>?@[\]^_`{|}~]{8,}

But it totally fails. Then what should be my correct pattern to do that?

I want Like...

Abc4k9h2L ->FALSE

Abc4k9hL+ ->TRUE

Abc4l@s$kl4 ->TRUE

And for this one, I've done a negative lookahead, that if it matches more than 3 digits, it won't be valid.

^(?=(.*\d){2})(?=.*[A-Z])(?=.*[a-z])(?!(.*\d){3,}).*$

Does this work for this one?

Ab45 -> Match
a4F8 -> Match
aosjgHJ35 -> Match
JsOsm4Osmf6 -> Match
JsO2m4Os4f6 -> No match (3 numbers)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...