Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@interaction/form-validator

Package Overview
Dependencies
Maintainers
6
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@interaction/form-validator

## 安装

  • 0.2.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
6
Created
Source

快速上手(FormValidator)

安装

npm i @interaction/form-validator

导入

import { FormValidator } from '@interaction/form-validator';

使用

表单实例化时,直接将验证加入。如下ts代码

export class Component {
  public form: FormGroup;
  constructor(
    private fb: FormBuilder
  ) {
    this.form = this.fb.group({
      input: [null, [FormValidator.phoneValidator()]]  // 这里使用
    });
  }
}

通过nz-form-explain标签捕获错误。如下html代码

<form nz-form [formGroup]="form" >
	<nz-form-item>
       <nz-form-label nzRequired>输入框</nz-form-label>
        <nz-form-control>
            <input 
               nz-input
               formControlName="input" />
            <nz-form-explain *ngIf="form?.get('input')?.dirty && form?.get('input')?.errors">
                <nz-form-explain *ngIf="form?.get('input')?.hasError('phone')">  // 这里是使用代码
                    只支持数字、字母或下划线
                </nz-form-explain>
            </nz-form-explain>
        </nz-form-control>
    </nz-form-item>
</form>

介绍

验证函数含义是否有参数参数默认值html错误字段
trimSidesValidator强制必填项(两侧不能有空格)hasError('trimSides')
phoneValidator手机号验证/^1[3-9]{2}[0-9]{8}$/hasError('phone')
cardIDValidator身份证号验证/^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}(\d|x|X)$/hasError('cardID')
emailValidator电子邮箱验证
(多邮箱用分号分隔)
/^((([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6};))*(([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})))$/hasError('email')
numValidator纯数字验证(包括小数)/^[0-9]+(.[0-9]+)?$/hasError('num')
enValidator纯英文验证/^[a-zA-z]+$/ihasError('en')
enNumUnderlineValidator只支持英文、数字和下划线/^\w+$/ihasError('enNumUnderline')
charMaxLength中英文字符最大长度255(最大长度默认为255)hasError('charMaxLength')
checkboxChecked验证checkbox选中1(至少一个被选中)hasError('noMatch')
checkboxCheckedAll验证checkbox必须全部选中hasError('noMatch')
notEmptyArray数组不为空验证1(至少一个不为空)hasError('empty')
notEmptyArrayAll数组中必须全部有值hasError('empty')
IPValidatorIP地址验证/^(([0-9]{0,3}).){3}([0-9]{0,3})$hasError('ip')
pathValidator文件路径验证/^/.*$/ihasError('path')
urlValidatorurl验证/^((http|ftp|https)://)?([\w-]+.)+[\w-]+([\w-./?%&=]*)?$/ighasError('url')

FAQs

Package last updated on 15 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc