Regular expression for "At least 8 Characters long include at least one letter and one numeric" using javascript. and string can have special characters .
Asked
Active
Viewed 287 times
-5
-
1You didn't bother to search first, did you? It has been asked 3 years ago, exactly the same question: http://stackoverflow.com/questions/7684815/regex-for-alpanumeric-with-at-least-1-number-and-1-character – Terry Oct 18 '14 at 05:50
-
I did but the issue was special characters and length. I tried adding pattern but it doesnt work. and i m not good in regex. :(. – Anupam Singh Oct 18 '14 at 05:54
-
1If you have tried anything, post your code here (even better: share it as a code snippet or a fiddle). That's what we are here to help — not to write code for you, but to check what could've gone wrong when you have a **specific** problem. – Terry Oct 18 '14 at 05:56
-
I tried ^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9_@./#&+-]+)$. – Anupam Singh Oct 18 '14 at 06:45
1 Answers
2
Edit, Updated
Note, Not entirely RegExp
approach
Try
var str = "#1abcdef"
, len = str.length
, t = len >= 8 && str.match(/[a-z0-9]/gi).some(isFinite);
console.log(t)
RegExp (updated, added)
/.{8}/.test(str) && !!str.match(/[0-9]{1}[a-z]{1}|[a-z]{1}[0-9]{1}/i)

guest271314
- 1
- 15
- 104
- 177
-
1Use `isFinite` instead of `Number`, as it will return `false` for `0`. – thefourtheye Oct 18 '14 at 05:58
-
1Also, `\w` includes `_` as well. So, this will allow even `#1________` – thefourtheye Oct 18 '14 at 05:58
-
1Apart from that, `trueFalse` is not a very good name for a variable. – thefourtheye Oct 18 '14 at 05:59
-
1Sorry for nitpicking. Checking the length first will be a small optimisation here. Upvoted. – thefourtheye Oct 18 '14 at 06:16
-
-
yes working , but seems just a working code not a complete regex. Actually I think we can make it better , let me try something withing regex. will update here once will get the better way. – Anupam Singh Oct 18 '14 at 06:38