正则表达式将特定的模式与数字匹配。允许数字0且大于5

时间:2019-09-20 14:29:58

标签: javascript regex

我想在输入字段中输入0或大于5的数字。

允许的号码: 0、5、10、11、12 ...

不允许的号码 负数1,2 3,4

 <span matPrefix>$ </span>
 <input id="hourlyrate" type="number" pattern="^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$"min="5"name="hourlyrate"#hourlyrate="ngModel"matInput[disabled]="!editRate"[(ngModel)]="skill.price"required>
 <span matSuffix>per 15 minutes</span>
 <mat-error *ngIf="hourlyrate.invalid">
   <span *ngIf="hourlyrate.errors?.required">This field is required.</span>
   <span *ngIf="hourlyrate.errors?.pattern">Minimum price should be $5.</span>
   <span *ngIf="hourlyrate.errors?.min">Minimum price should be $5.</span>
 </mat-error>

1 个答案:

答案 0 :(得分:1)

我会使用:

^(?![1234]$)\d+$

说明:

^               # beginning of string
    (?!         # negative lookahead, make sure we haven't after:
        [1234]  # one of the digit 1 or 2 or 3 or 4
        $       # end of string
    )           # end lookahead
    \d+         # 1 or more digits
$               # end of string