代码拉取完成,页面将自动刷新
English | 在线使用 | 更新日志 | 反馈错误/缺漏 | Gitee
npm i pure-v
import purev from 'pure-v';
purev('2020-01-01', 'date');
<script src="https://cdn.jsdelivr.net/npm/pure-v/purev.min.js"></script>
<script>
purev('2020-01-01', 'date');
</script>
interface IPureV {
(text: TValidText, rule: TRule): ITextValidResult;
(json: Json<TValidText>, rules: Json<TRule>): IJsonValidResult;
(formName: string | HTMLElement): IFormValidResult;
useToast: boolean;
toast(text: string): void;
tip(rule: string | Json<string>, text: string): void;
reg(rule: string | Json<IReg>, reg?: IReg): void;
lang(lang: TLang): void | never;
onOnePass(option: IBaseValidResult, dom?: HTMLElement): void;
onOneFail(option: IBaseValidResult, dom?: HTMLElement): void;
}
purev('2020-01-02', 'date')
purev({
name: 'theajack',
birthday: '1994-01-01',
email: 'me@theajack.com',
intro: ''
}, {
name: 'notnull',
birthday: 'date',
email: 'email',
intro: 'notnull'
});
<div pv-form='form'>
name:<input type="text" pv-rule='notnull'><br><br>
birthday:<input type="text" pv-rule='date'><br><br>
<button onclick='valid()'>validate</button>
</div>
<script>
function valid (){
purev('form');
}
</script>
支持使用 pv-form 属性,css选择器,或dom元素
当元素有 pv-rule 属性时,则只验证当前元素,否则验证该元素所有含有 pv-rule 属性的子元素
有以下属性
purev.reg('custom', /^\d{3,4}$/);
purev.reg('customFn', (v) => {
return v === 'purev' || v === 'PUREV';
});
purev('123', 'custom').pass,
purev('aaa', 'custom').pass,
purev('12345', 'custom').pass,
purev('purev', 'customFn').pass,
purev('PUREV', 'customFn').pass,
purev('xxxxx', 'custom').pass,
purev.tip('date', '自定义日期提示');
purev('xxx', 'date').message;
purev.onOnePass = (result) => {
// ...
};
purev.onOneFail = (result) => {
// ...
};
onOnePass 和 onOneFail 为单例模式,如需设置请直接覆盖这两个属性
const result = {
notnull: [
purev('', 'notnull').pass,
purev('xx', 'notnull').pass,
],
date: [
purev('xx', 'date').pass,
purev('2020-01-02', 'date').pass,
purev('2020-13-02', 'date').pass,
],
email: [
purev('theajack@qq.com', 'email').pass,
purev('xx', 'email').pass,
],
number: [
purev('1', 'number').pass,
purev('12', 'number').pass,
purev('12.3a', 'number').pass,
purev('a12.3', 'number').pass,
purev('123', 'number[3]').pass,
purev('1234', 'number[3]').pass,
purev('12345', 'number[3,6]').pass,
],
idcard: [
purev('340827111111111111', 'idcard').pass,
purev('34082711111111111X', 'idcard').pass,
purev('3408271111111111111', 'idcard').pass,
],
length: [
purev('123456', 'length[6]').pass,
purev('1234你好', 'length[6]').pass,
purev('1234567', 'length[6,9]').pass,
],
url: [
purev('https://www.baidu.com', 'url').pass,
purev('http://www.baidu.com', 'url').pass,
purev('xxxx', 'url').pass,
],
decimal: [
purev('1.1', 'decimal').pass,
purev('0.1', 'decimal').pass,
purev('0.1a', 'decimal').pass,
purev('11', 'decimal').pass,
],
lengthOfAny: [
purev('123456', 'lengthOfAny[6]').pass,
purev('1234你好', 'lengthOfAny[6]').pass,
purev('12345你好', 'lengthOfAny[6]').pass,
],
phone: [
purev('11111111111', 'phone').pass,
purev('1234', 'phone').pass,
purev('22222222222', 'phone').pass,
],
letterStart: [
purev('a12', 'letterStart').pass,
purev('a121', 'letterStart[4]').pass,
purev('a121', 'letterStart[3, 5]').pass,
purev('a1212a', 'letterStart[3, 5]').pass,
],
range: [
purev('99', 'range[100, 200]').pass,
purev('123', 'range[100, 200]').pass,
purev('200', 'range[100, 200]').pass,
purev('201', 'range[100, 200]').pass,
],
express: [
purev('123', 'express[^\\d{3,4}$]').pass,
purev('aaa', 'express[^\\d{3,4}$]').pass,
purev('12345', 'express[^\\d{3,4}$]').pass,
],
withNull: [
purev('11111111111', 'phone').pass,
purev('', 'phone').pass,
purev('', 'phone null').pass,
]
};
purev({
name: 'theajack',
birthday: '1994-01-01',
email: 'me@theajack.com',
intro: ''
}, {
name: 'notnull',
birthday: 'date',
email: 'email',
intro: 'notnull'
})
purev.reg('custom', /^\d{3,4}$/);
purev.reg('customFn', (v) => {
return v === 'purev' || v === 'PUREV';
});
export interface IPureV {
(text: TValidText, rule: TRule): ITextValidResult;
(json: Json<TValidText>, rules: Json<TRule>): IJsonValidResult;
(formName: string | HTMLElement): IFormValidResult;
useToast: boolean;
toast(text: string): void;
tip(rule: string | Json<string>, text: string): void;
reg(rule: string | Json<IReg>, reg?: IReg): void;
lang(lang: TLang): void | never;
onOnePass(option: IBaseValidResult, dom?: HTMLElement): void;
onOneFail(option: IBaseValidResult, dom?: HTMLElement): void;
}
interface IBaseValidResult {
pass: boolean;
message: string;
name?: string;
reg?: RegExp;
text: string;
}
export interface ITextValidResult extends IBaseValidResult {}
export interface IJsonValidResult {
pass: boolean;
message?: string;
results: {
[prop in string]: IBaseValidResult;
}
}
interface IDomValidResult extends IBaseValidResult {
dom: HTMLElement
}
export interface IFormValidResult {
pass: boolean;
message?: string;
results: {
[prop in string]: IDomValidResult;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。