[Angular] Custom directive Form validator

Create a directive to check no special characters allowed:

import {Directive, forwardRef} from ‘@angular/core‘;
import {AbstractControl, NG_VALIDATORS, Validator} from ‘@angular/forms‘;
@Directive({
  selector: `[formControl][no-special-chars],
             [formControlName][no-special-chars],
             [ngModel][no-special-chars]`,
  providers: [
    {
      multi: true,
      provide: NG_VALIDATORS,
      useExisting: forwardRef(() => NoSpecialCharsValidator)
    }
  ]
})

export class NoSpecialCharsValidator implements Validator {
  validate(c: AbstractControl): { [key: string]: any; } {
    const res = /[~`!#$%\^&*+=\-\[\]\\‘;,/{}|\\":<>\?]/g.test(c.value);
    return res ? {special: true}: null;
  }
}
        <div class="meal-form__name">
          <label>
            <h3>Meal name</h3>
            <input type="text"
                   no-special-chars
                   formControlName="name"
                   placeholder="e.g. English Breakfast">
            <div class="error" *ngIf="noSpecials">
              Cannot contain special characters
            </div>
          </label>
        </div>
  get noSpecial() {
    return (
      this.form.get(‘name‘).hasError(‘special‘) &&
        this.form.get(‘name‘).touched
    );
  }
时间: 2024-10-14 08:16:29

[Angular] Custom directive Form validator的相关文章

[Angular 2] Directive intro and exportAs

First, What is directive, what is the difference between component and directive. For my understanding, component is something like 'canvas', 'form', 'table'... they have the template and their own functionality. It defines how a html tag should work

angular使directive让div contenteditable & ng-model生效

.directive("contenteditable", function(){   return {     restrict: "A",     require: "ngModel",     link: function(scope, element, attrs, ngModel) {       function read() {         ngModel.$setViewValue(element.html());      

关于angular 自定义directive

关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp",[]).directive("expander",function(){ return{ //directive的一些属性(键值对形式)如下: /* restrict:'EA', replace:true, transclude:true, scope:{...}, templ

cv.form.validator

有即时验证,加载完验证两种方式: 即时验证: cv.form.validator(jQuery("#qq").val(),{ type:["require","intlength","int"],min:5,max:11, errorFunction:function(obj,options){ switch(options.type){ case "require" :{ jQuery("#ms

Create Custom Modification Form In VS 2012-Part2

1.SPWorkflowModification ContextData is XMLSerialized as String. 2.Get SPWorkflowModification ContextData in modification page protected void GetContexData()        {            SPWeb currentWeb = SPContext.Current.Web;            string strWorkflowI

angular service/directive

<html class=" js cssanimations csstransitions" ng-app="phonecatApp" > <head> <title>{{title}}</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name=&

解决Sharepoint 2010 custom display form 不显示附件的问题

sharepoint 2010用designer添加自定义的 display form默认是不会显示附件的. 需要添加如下代码才会显示附件: <tr> <td width="190px" valign="top" class="ms-formlabel"> <H3 class="ms-standardheader"> <nobr>Attachment</nobr> &

Create Custom Modification Form In VS 2012-Part1

Step1.Add EventHandlingScope Activity Under OnWorkflowActivated Step2.Add SequenceActivity In EventHandlingScope Activity Step3.Add EnableWorkflowModification Activity a.Bind [ContextData] to a new field b.Set [Colorrelation Token] as "Modification&q

angular $http 与form表单的select--&gt;refine

<!DOCTYPE html> <html ng-app="a2_15"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"