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
- It can not validate string length.
- hyphen can be in last position
2
Answers
You can use lookahead to validate string length:
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.
Try once,
OR
This regular expression will match any string that starts with "abc" and is followed by one or more alphanumeric characters.