当前位置:   article > 正文

antd 表单的校验方式_antd表单校验

antd表单校验

 

antd 表单的两种校验方式

1.声明式表单验证:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

                        <Form.Item

                            name="username"

                            rules={[

                                {

                                    required: true,

                                    message: 'Please input your Username!',

                                },

                                {

                                    max: 20,

                                    message: '最长20位!',

                                },

                                {

                                    min: 5,

                                    message: '至少5位!!',

                                },                                {

                                    pattern: /^[A-Za-z\d_]+$/,

                                    message: '自能包含字母数字下划线字符!',

                                },

                            ]}

                        >

                            <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="Username" />

                        </Form.Item>

2. validator自定义式验证:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

                        <Form.Item

                            name="password"

                            rules={[

                                {

                                    validator: (_, value) =>{

                                    if(value.length >= 6 && value.length<=10) {

                                        return Promise.resolve()

                                    }else{

                                        return Promise.reject('密码长度必须是6~10位')

                                    }

                                  }

                                }

                            ]}

                        >

                            <Input

                                prefix={<LockOutlined className="site-form-item-icon" />}

                                type="password"

                                placeholder="Password"

                            />

                        </Form.Item>

antd-design-vue form表单验证,禁止输入中文,只数字,等验证要怎么写呀~~

因为是在表单里写的,所以有点迷,不知道要怎么写..
请大神给个思路~~~
  1. <a-form-item v-bind="formStyle" label="别名(英文)">
  2. <a-input
  3. placeholder="请输入别名"
  4. v-decorator="['menuPath', { rules: [
  5. { required: true, message: '请输入别名' },
  6. { validator: (rule, value, cb) => this.changeKey(rule, value, cb) }
  7. ] }]"
  8. />
  9. </a-form-item>
把采纳给了楼下
我现在的解决办法如下: 感觉这样更方便点儿
  1. <a-form-item v-bind="formStyle" label="别名(英文)">
  2. <a-input
  3. placeholder="请输入别名"
  4. maxlength="50"
  5. minlength="3"
  6. v-decorator="['menuPath', { rules: [
  7. { required: true, message: '请输入别名' }
  8. ],
  9. getValueFromEvent: (event) => {
  10. return event.target.value.replace(/[\u4E00-\u9FA5]/g,'')
  11. }
  12. }]"
  13. />
  14. </a-form-item>

相关代码

// 只列举一个表单项,其他的规则都是必填。

  1. <a-form :form="form" @submit="handleSubmit" layout="vertical">
  2. <a-form-item
  3. label="标题"
  4. :labelCol="formItemLayout.labelCol"
  5. :wrapperCol="formItemLayout.wrapperCol">
  6. <a-input
  7. v-decorator="[
  8. 'title',
  9. {initialValue: formData.title, rules: [{ required: true, message: '请输入标题' }]}
  10. ]"
  11. placeholder="标题" />
  12. </a-form-item>
  13. </a-form>

// 提交

  1. async handleSubmit (e) {
  2. event.preventDefault()
  3. this.form.validateFields((err, values) => {
  4. if (!err) {
  5. Object.assign(this.formData, values)
  6. Object.assign(this.formData, { updatedAt: Date.now() })
  7. console.log('提交信息成功', this.formData)
  8. }
  9. })
  10. if (this.formData.id) {
  11. const res = await updateBanner(this.formData)
  12. console.log('编辑', res)
  13. } else {
  14. const res = await createBanner(this.formData)
  15. console.log('新增', res)
  16. }
  17. },


以下是一段手机号的的自定义校验,在提交表单的时候form.validateFields总是出现错误,方法无法向下执行Vue+Antd表单校验form.validateFields执行错误的解决方法

在自定义校验里面每一个判断都要调用callback(),要保证方法最后执行到的永远都是callback()

//自定义校验(检查手机号是否输入正确)
checkPhone(rule, value, callback) {
  if (value != null && value != "") {
    var reg = /^1[3456789]\d{9}$/;
    if (!reg.test(value)) callback(new Error("手机号格式不正确"));
    //调用callback
    else callback();
  } else {
    callback();
  }
}
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/115579
推荐阅读
相关标签
  

闽ICP备14008679号