Need regex for the “Project name” with following condition. Expected Result: Take alphabetical, alpha numeric, alpha numeric symbolic Actual Result:
JavaScript
x
6
1
1. Take alpha – Pass
2
2. Take Numeric – Pass
3
3. Take Alpha numeric – Pass
4
4. Take alpha numeric special characters - Pass
5
5. If Only special characters – Fail
6
that is, Everything is allowed but if it is just special characters and no text than it’s a problem. **Example:
- zjna5726$7&^#bsg //allowed
- %&*% // just special characters not allowed
- I tried /^[ A-Za-z0-9_@./#&+-]*$/ but did not help
- ^([w+d+]+)((-)([w+d+]+))* //did not help it is only excepting one character in between only.
Advertisement
Answer
^(?=[W_]+[a-zA-Z0-9]|[a-zA-Z0-9]+[W_]|[a-zA-Z0-9]+).+$
– This regex should fulfil your requirement.
JavaScript
1
15
15
1
alpha="abc"
2
numeric="123456789"
3
alphaNumeric="Abc1234"
4
alphaNumericSpcl="#Abc1234$!@~!"
5
onlySpcl="!@#$%^&*()_-+={}|][:;'".,><?/`"
6
randomString="!@#$%^&*()_-abde9+={}|][:;'".,><?/`"
7
8
regex=/^(?=[W_]+[a-zA-Z0-9]|[a-zA-Z0-9]+[W_]|[a-zA-Z0-9]+).+$/
9
console.log("Test of Alpha:",regex.test(alpha));
10
console.log("Test of Numeric:",regex.test(numeric));
11
console.log("Test of Alphanumeric:",regex.test(alphaNumeric));
12
console.log("Test of Alphanumeric and Special:",regex.test(alphaNumericSpcl));
13
console.log("Test of only Special:",regex.test(onlySpcl));
14
console.log("Test of Random String:",regex.test(randomString));
15
Output:
JavaScript
1
7
1
Test of Alpha: true
2
Test of Numeric: true
3
Test of Alphanumeric: true
4
Test of Alphanumeric and Special: true
5
Test of only Special: false
6
Test of Random String: true
7