美文网首页
表单校验 2021-10-13

表单校验 2021-10-13

作者: Kawing_Zhang | 来源:发表于2021-10-13 16:43 被阅读0次

```

  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;

                }

            },

```

相关文章

网友评论

      本文标题:表单校验 2021-10-13

      本文链接:https://www.haomeiwen.com/subject/faceoltx.html