skip to Main Content

I want to disable button if entered string is not like G6A123-4853473

Validation conditions :
string should start with G6
After that it should contain one or more alphabets
and should contain one hyphen (-) , hyphen should not be on last position
and total length of string should be 14

I am using angular , I have tried this ^([G6][A-Z]*[0-9]*[-][0-9]*)$

<input matInput type="search" #filter>
<button mat-raised-button color="primary" [disabled]="!filter.value.match('^([8D][A-Z]*[0-9]*[-][0-9]*)$')"> Search 
</button>

The problem in this are

  1. It can not validate string length.
  2. hyphen can be in last position

2

Answers


  1. You can use lookahead to validate string length:

    ^(?=.{14}$)G6(?=.*[A-Z])[A-Z0-9]+-[A-Z0-9]+
    

    It requires that after beginning of the string there are 14 symbols and end of the string.

    (?=.*[A-Z]) requires presence of upper letter somewhere after G6.

    Also, to check that there are digits after hyphen you can use + qualifier.

    Demo here.

    Login or Signup to reply.
  2. Try once,

    ^abc([a-zA-Z0-9]+)$
    

    OR

    ^G6[A-Za-z]+-[A-Za-z0-9]{7}$
    

    This regular expression will match any string that starts with "abc" and is followed by one or more alphanumeric characters.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search