npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

maishu-dilu

v1.11.1

Published

表单验证框架

Downloads

10

Readme

DILU

表单验证框架

使用

  • 传统 JS 项目使用,引用 dilu.js
<script src="js/dilu.js"></script>
  • 使用 AMD 加载
requirejs.config({
    shim: {
        dilu: {
            exports: 'dilu'
        }
    },
    paths: {
        dilu: 'js/dilu'
    }
}
requirejs(['dilu'],function(dilu){

})

示例

HTML

<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
</head>

<body>
    <div class="container form-horizontal col-xs-12 col-sm-6" style="padding-top:10px;">
        <div class="form-group">
            <label class="col-xs-12 col-sm-3">手机号码</label>
            <div class="col-xs-12 col-sm-9">
                <input name="mobile" name="手机号码" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-xs-12 col-sm-3">验证码</label>
            <div class="col-xs-12 col-sm-9">
                <div class="input-group">
                    <input name="verifyCode" class="form-control" />
                    <span class="input-group-btn">
                        <button id="btn-verifyCode" class="btn btn-default" type="button">发送验证码</button>
                    </span>
                </div>
                <div id="verifyCodeError" class="validateMessage"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-xs-12 col-sm-3">密码</label>
            <div class="col-xs-12 col-sm-9">
                <input name="password" type="password" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-xs-12 col-sm-3">确认密码</label>
            <div class="col-xs-12 col-sm-9">
                <input name="confirmPassword" type="password" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-12"></div>
            <div class="col-xs-12" style="text-align:right;">
                <button id="btn-register" class="btn btn-primary btn-block">注册</button>
            </div>
        </div>
    </div>

</body>
<script src="js/dilu.js"></script>
<script src="index.js"></script>

</html>

JS

var FormValidator = dilu.FormValidator, r = dilu.rules;
var passwordInput = document.getElementsByName('password')[0];
var form = document.getElementsByClassName('container')[0];
var validator = new dilu.FormValidator(form,
    { name: "mobile", rules: [r.required(), mobile()] },
    {
        name: 'verifyCode',
        errorElement: document.getElementById('verifyCodeError'),
        rules: [required()], depends: [function () { return validator.checkElement('mobile'); }]
    },
    { name: 'password', rules: [required('请输入密码')] },
    {
        name: 'confirmPassword',
        rules: [
            required('请再次输入密码'),
            equal(function () { return passwordInput.value; }, '两次输入的密码不一致')
        ]
    });
document.getElementById('btn-register').onclick = function () {
    validator.check();
};
document.getElementById('btn-verifyCode').onclick = function () {
    validator.checkElement('mobile');
};

验证方法

验证所有表单字段

check(): Promise<boolean>

验证单个表单字段

checkElement(inputElement: HTMLInputElement): Promise<boolean>

验证规则

名称 | 说明 ----------------------- | ----------------------- required|验证必填字段 email |验证邮箱 equal |验证字段与某个值相等 greaterThan |验证字段大于某个值 lessThan |验证字段小于某个值 custom |自定义验证规则

示例

API

Formvalidator 对象

表单验证器,用于对表单中的字段进行验证

check 方法

对验证表单中的元素进行同步验证。

定义

check(): boolean;

示例

let validator = new dilu.FormValidator(dialog_element,
    { name: 'fileName', rules: [dilu.rules.required('请输入文件名')] }
);
let isValid = validator.check();

checkAsync 方法

对验证表单中的元素进行异步验证。

定义

check(): Promise<boolean>;

示例

let validator = new dilu.FormValidator(dialog_element,
    { name: 'fileName', rules: [dilu.rules.required('请输入文件名')] }
);
validator.checkAsync().then(function(isValid){

});

clearErrors 方法

清除表单的错误信息

定义

clearErrors(): void;

示例

let validator = new dilu.FormValidator(dialog_element,
    { name: 'fileName', rules: [dilu.rules.required('请输入文件名')] }
);
validator.clearErrors();

clearElementError 方法

定义

clearElementError(name: string): void

checkElementAsync

异步验证 HTML 元素

定义

checkElementAsync(name: string): Promise<boolean>

checkElement

同步验证 HTML 元素

定义

checkElement(name: string): boolean;

rules 对象

表单验证规则

required 方法

验证必填字段

定义

required(error?: string): Rule

matches 方法

验证两个字段值是否相等

matches(otherElement: InputElement, error?: string) => Rule

email 方法

验证邮箱

email(error?: string): Rule

minLength 方法

验证字段最小长度

定义

minLength(length: number, error?: string): Rule

maxLength 方法

验证字段的最大长度

定义

maxLength(length: number, error?: string): Rule