Raktim Posted September 10, 2019 Share Posted September 10, 2019 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 10, 2019 Share Posted September 10, 2019 Make 2 patterns for 1 digit each instead of 1 pattern for 2 digits. Link to comment Share on other sites More sharing options...
Raktim Posted September 10, 2019 Author Share Posted September 10, 2019 (edited) 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 September 10, 2019 by Raktim Link to comment Share on other sites More sharing options...
justsomeguy Posted September 10, 2019 Share Posted September 10, 2019 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. Link to comment Share on other sites More sharing options...
Raktim Posted September 11, 2019 Author Share Posted September 11, 2019 okay Link to comment Share on other sites More sharing options...
Funce Posted September 11, 2019 Share Posted September 11, 2019 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 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted September 11, 2019 Share Posted September 11, 2019 It looks like that should work. 1 Link to comment Share on other sites More sharing options...
Raktim Posted September 12, 2019 Author Share Posted September 12, 2019 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. Link to comment Share on other sites More sharing options...
Raktim Posted September 12, 2019 Author Share Posted September 12, 2019 (edited) 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 September 12, 2019 by Raktim Link to comment Share on other sites More sharing options...
justsomeguy Posted September 12, 2019 Share Posted September 12, 2019 Use ^ at the start a character class to negate it. [^0-9] means any character other than a digit. 1 Link to comment Share on other sites More sharing options...
Raktim Posted September 12, 2019 Author Share Posted September 12, 2019 (edited) 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 September 12, 2019 by Raktim Link to comment Share on other sites More sharing options...
Raktim Posted September 12, 2019 Author Share Posted September 12, 2019 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? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 12, 2019 Share Posted September 12, 2019 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. Link to comment Share on other sites More sharing options...
Raktim Posted September 13, 2019 Author Share Posted September 13, 2019 (edited) 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? Edited September 13, 2019 by Raktim Link to comment Share on other sites More sharing options...
Raktim Posted September 13, 2019 Author Share Posted September 13, 2019 (edited) 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 September 13, 2019 by Raktim Link to comment Share on other sites More sharing options...
justsomeguy Posted September 13, 2019 Share Posted September 13, 2019 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. 1 Link to comment Share on other sites More sharing options...
Raktim Posted September 13, 2019 Author Share Posted September 13, 2019 Okay Sir Link to comment Share on other sites More sharing options...
Raktim Posted September 15, 2019 Author Share Posted September 15, 2019 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 Link to comment Share on other sites More sharing options...
Funce Posted September 15, 2019 Share Posted September 15, 2019 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) 1 Link to comment Share on other sites More sharing options...
Raktim Posted September 24, 2019 Author Share Posted September 24, 2019 Yes, both work fine. Tanks Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now