-1

I have to validate an input in html using pattern in angular 4 my pattern is

pat = /^(\s*#?\d{1,2}:\d{1,2}\s*(am|pm|AM|PM)\s*,?)+/;

and in html

<input type="text" [pattern]="precisionTimePattern" #precisionSlotValue="ngModel" name="slot-value-precision" 
            id="slotValuePrecision" class="input-minimal" 
            [ngModel]="formObject.precisionType.slotValue" 
            [class.invalid]="(!precisionSlotValue.valid && precisionSlotValue.touched) || (!precisionSlotValue.valid && f.submitted)">

to match string only if "12:00 AM, 2:0 PM #3:00 PM"
but it also validate if user input "12:00 AM, 2:0 PM #3:00 PM ajkcsbkajk"

Piyush Patel
  • 1,212
  • 15
  • 19

1 Answers1

0

Just add a dollar sign (end of string match)

pat = /^(\s*#?\d{1,2}:\d{1,2}\s*(am|pm|AM|PM)\s*,?)+$/;
smnbbrv
  • 23,502
  • 9
  • 78
  • 109