0

I tried with many patters for username in my Angular5 application. But didn't get a suitable solution for my requirement.

The Rules are

Minimum 6 characters
Only numbers are not allowed at least one character should be there
No special characters allowed except _
No space allowed
Character only is allowed

I tried with /^[a-zA-Z0-9]+([_]?[a-zA-Z0-9])*$/

/^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![_\s\-]{6,}.*)$/
7
  • 4
    What did you try? where is your code/efforts? Commented Jun 1, 2018 at 0:57
  • 1
    Could you add your code? Commented Jun 1, 2018 at 1:05
  • I have handled minimum number separately because I want to display a message for that case. My issue is only number case is not able to validate. @VicJordan Allan Commented Jun 1, 2018 at 1:12
  • Is this fine? "/^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![-?\d+\.?\d*$]{6,}.*)$/" Commented Jun 1, 2018 at 1:20
  • 1
    Yes @Allan. Thank you.. :) Commented Jun 1, 2018 at 9:37

3 Answers 3

10

You can try this regex:

^[a-zA-Z0-9_]{5,}[a-zA-Z]+[0-9]*$
  • [a-zA-Z0-9_]{5,} to match at least five alphanumerics and the underscore
  • [a-zA-Z]+ to have at least one letter
  • [0-9]* to match zero to any occurrence of the given numbers range

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use the following regex:

^(?=[a-z_\d]*[a-z])[a-z_\d]{6,}$

in case insensitive mode as tested on regex101: demo

Explanations:

  • ^ anchor for the beginning of the string
  • $ anchor for the end of the string
  • (?=[a-z_\d]*[a-z]) to force the presence of at least one letter
  • [a-z_\d]{6,} implement the at least 6 char constraint

Comments

-1

Yes this is fine for me. Thanks./^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![-?\d+\.?\d*$]{6,}.*)$/

1 Comment

No fail for this. "5mm656565v5". :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.