# 如何验证数据

DartMars 默认提供了一些常用的验证数据的方法,封装在验证帮助类 VerifyHelper

使用 VerifyHelper 相关类,需要先导入 VerifyHelper 类,假设你的项目名为 project_name, 则需要如下导包

import 'package:project_name/bootstrap/helper/VerifyHelper.dart';
1

例如验证某字符串是不是空字符串

String s1 = '';
true == VerifyHelper.empty(s1);

String s2;
true == VerifyHelper.empty(s2);
1
2
3
4
5

例如验证某数值是不是等于 0

int n1 = 0;
true == VerifyHelper.zero(n1);

int n2;
true == VerifyHelper.zero(n2);
1
2
3
4
5

更多其他方法,参看下表

方法名称 作用
empty 返回 如果字符串 [s] 为空, 否则返回 .
notEmpty 返回 如果字符串 [s] 不为空, 否则返回 .
zero 返回 如果数值 [n] = 0, 否则返回 .
notZero 返回 如果数值 [n] != 0, 否则返回 .
positive 返回 如果数值 [n] > 0, 否则返回 .
positiveInt 返回 如果数值 [n] > 0, 否则返回 .
positiveDouble 返回 如果数值 [n] > 0, 否则返回 .
notPositive 返回 如果数值 [n] <= 0, 否则返回 .
notPositiveInt 返回 如果数值 [n] <= 0, 否则返回 .
notPositiveDouble 返回 如果数值 [n] <= 0, 否则返回 .
negative 返回 如果数值 [n] < 0, 否则返回 .
negativeInt 返回 如果数值 [n] < 0, 否则返回 .
negativeDouble 返回 如果数值 [n] < 0, 否则返回 .
notNegative 返回 如果数值 [n] >= 0, 否则返回 .
notNegativeInt 返回 如果数值 [n] >= 0, 否则返回 .
notNegativeDouble 返回 如果数值 [n] >= 0, 否则返回 .
lengthBetween 返回 如果字符串 [s] 的长度在 [min] 与 [max] 之间, 否则返回 .
lengthNotBetween 返回 如果字符串 [s] 的长度不在 [min] 与 [max] 之间, 否则返回 .
lengthEq 返回 如果字符串 [s] 的长度等于[length], 否则返回 .
lengthNotEq 返回 如果字符串 [s] 的长度不等于[length], 否则返回 .
valueBetween 返回 如果数值 [n] 的值在 [min] 与 [max] 之间, 否则返回 .
valueNotBetween 返回 如果数值 [n] 的值不在 [min] 与 [max] 之间, 否则返回 .
valueEq 返回 如果数值 [n] 的值等于 [value], 否则返回 .
valueNotEq 返回 如果数值 [n] 的值不等于 [value], 否则返回 .
email 返回 如果字符串 [s] 是电子邮箱格式, 否则返回 .
notEmail 返回 如果字符串 [s] 不是电子邮箱格式, 否则返回 .
chsMobile 返回 如果字符串 [s] 是中国手机号码, 否则返回 .
notChsMobile 返回 如果字符串 [s] 不是中国手机号码, 否则返回 .
chsIdCard 返回 如果字符串 [s] 是中国身份证号, 否则返回 .
notChsIdCard 返回 如果字符串 [s] 不是中国身份证号, 否则返回 .
alpha 返回 如果字符串 [s] 是英文字母, 否则返回 .
notAlpha 返回 如果字符串 [s] 不是英文字母, 否则返回 .
alphaNum 返回 如果字符串 [s] 是英文字母,数字, 否则返回 .
notAlphaNum 返回 如果字符串 [s] 不是英文字母,数字, 否则返回 .
alphaNumDash 返回 如果字符串 [s] 是英文字母,数字,-,_, 否则返回 .
notAlphaNumDash 返回 如果字符串 [s] 不是英文字母,数字,-,_, 否则返回 .
chs 返回 如果字符串 [s] 是中文, 否则返回 .
notChs 返回 如果字符串 [s] 不是中文, 否则返回 .
chsAlpha 返回 如果字符串 [s] 是中文,英文字母, 否则返回 .
notChsAlpha 返回 如果字符串 [s] 不是中文,英文字母, 否则返回 .
chsAlphaNum 返回 如果字符串 [s] 是中文,英文字母,数字, 否则返回 .
notChsAlphaNum 返回 如果字符串 [s] 不是中文,英文字母,数字, 否则返回 .
chsAlphaNumDash 返回 如果字符串 [s] 是中文,英文字母,数字,-,_, 否则返回 .
notChsAlphaNumDash 返回 如果字符串 [s] 不是中文,英文字母,数字,-,_, 否则返回 .