表单校验 2021-10-13

```

  fromValidation(type, param) {

                var nothing;

                switch (type) {

                    case "cxrs":

                        nothing = this.validation("nothing", param); // 校验空格

                        if(nothing || param == "") {

                            this.errorMessage.cxrs = "不能为空或空格";

                        } else {

                            this.errorMessage.cxrs = "";

                        }

                        break;

                    case "pxmd":

                        const pxmd = this.validation("NonnegativeFloatingNumber", param); // 非负浮点数

                        if (pxmd) {

                            this.errorMessage.pxmd = "";

                        } else {

                            this.errorMessage.pxmd = "请填写收入";

                        }

                        break;

                    case "BAccumulated":

                        const BAccumulated = this.validation("NonnegativeFloatingNumber", param); // 非负浮点数

                        if (BAccumulated) {

                            this.errorMessage.BAccumulated = "";

                        } else {

                            this.errorMessage.BAccumulated = "请填写累计保额";

                        }

                        break;

                }

            },

            //表单校验规则

            validation(type, param) {

                switch (type) {

                    //校验名字

                    case "name":

                        // eslint-disable-next-line no-case-declarations

                        const isTrueChineseName = this.validChineseName(param); // 校验中文名字

                        // eslint-disable-next-line no-case-declarations

                        const isTrueEnglishName = this.validEnglishName(param); // 校验英文名字

                        if (isTrueChineseName || isTrueEnglishName) {

                            return true;

                        } else {

                            return false;

                        }

                        // eslint-disable-next-line no-unreachable

                        break;

                    //判断电话号码是否合法

                    case "phoneNumber":

                        // eslint-disable-next-line no-case-declarations

                        const phoneNumber = /^1[3456789]\d{9}$/.test(param);

                        if (phoneNumber) {

                            return true;

                        } else {

                            return false;

                        }

                        // eslint-disable-next-line no-unreachable

                        break;

                    //邮箱正确?

                    case "email":

                        // eslint-disable-next-line no-case-declarations

                        const email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(

                            param

                        );

                        if (email) {

                            return true;

                        } else {

                            return false;

                        }

                        // eslint-disable-next-line no-unreachable

                        break;

                    //两位小数,钱

                    case "money":

                        // eslint-disable-next-line no-case-declarations

                        const money = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(

                            param

                        );

                        if (money) {

                            return true;

                        } else {

                            return false;

                        }

                        // eslint-disable-next-line no-unreachable

                        break;

                    //非负浮点数

                    case "NonnegativeFloatingNumber":

                        // eslint-disable-next-line no-case-declarations

                        const NonnegativeFloatingNumber = /^\d+(\.\d+)?$/.test(param);

                        if (NonnegativeFloatingNumber) {

                            return true;

                        } else {

                            return false;

                        }

                        // eslint-disable-next-line no-unreachable

                        break;

                    //空格?

                    case "nothing":

                        // eslint-disable-next-line no-case-declarations

                        const nothing = /\s+/g.test(param);

                        if (nothing) {

                            return true;

                        } else {

                            return false;

                        }

                        // eslint-disable-next-line no-unreachable

                        break;

                }

            },

```

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • /** * 参考文档 * 【eslint英文文档】https://eslint.org/docs/user-gui...
    故事_71b0阅读 1,421评论 0 0
  • eslint配置方式 注释配置:使用js注释来直接嵌入ESLint配置信息到一个文件里 配置文件:使用一个js文件...
    轻轻地走在路上阅读 1,510评论 0 0
  • eslint配置方式有两种: 注释配置:使用js注释来直接嵌入ESLint配置信息到一个文件里 配置文件:使用一个...
    tenwh阅读 107评论 0 0
  • 用更合理的方式写 JavaScript 目录 声明变量 对象 数组 字符串 函数 箭头函数 模块 迭代器和生成器 ...
    小红依阅读 1,834评论 0 5
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,606评论 28 53