0

I want that it should start with alphabet and it's up to user if he/she wants to add numerical but there should be no special characters and should start with alphabet.

I tried "^[A-Za-z][0-9]+$" but it's not working..

Correct format:

  ASDFG123 
  asdfg123 
  a1231sadas 

Wrong format:

  asdfg_123 
  Asdfg-31

1 Answer 1

1

You could try the below regex, in which the string must be start with alphabets followed by one or more digits and again followed by zero or more times alphabets or numbers.

^[A-Za-z]+[0-9]+[A-Za-z0-9]*$

DEMO

You could try the below regex if the input may or maynot contain numbers,

^[A-Za-z]+[A-Za-z0-9]*$

DEMO

Pattern Explanation:

  • ^ Asserts that we are at the start.
  • [A-Za-z]+ Allows one or more times alphabets(both uppercase and lowercase). + means repeat the preceding token one or more times. In our case the preceding token is the character class.
  • [A-Za-z0-9]* Allows zero or more times alphabets or numbers. * means repeat the preceding token zero or more times.
  • $ End of the line.
Sign up to request clarification or add additional context in comments.

1 Comment

The numeric is optional. The user can input without or with numeric in the middle of the string but there should be no special characters and it should start with alphabet

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.