antd 規則檢查


 antd 兩種正則檢查

1.getFieldDecorator  rules[] 內置函數

 <FormItem {...formItemLayout} label="規則名稱">
     {getFieldDecorator('ruleName', {
      validateTrigger:"onBlur",
      rules: [
          (rule, value, callback, source, options) => {
             const errors = []
             if(value){
                if (!/^(?=[0-9a-zA-Z\u4e00-\u9fa5]+$)/.test(value)) {
                   errors.push(new Error(value + '不是合法數值', rule.field))
                }
             }else{
                errors.push(new Error('請輸入規則名稱', rule.field))
             }
                 callback(errors)
             }]
            })(<Input type="text" onBlur={this.searchRepeat.bind(this)} />)}
 </FormItem>

 2.getFieldDecorator  rules[] 支持validator,調用函數,還有antd定義的 數字,字母,最大,最小等等(官網可看)

<FormItem>
{getFieldDecorator('ip1_edit', {
    validateTrigger:"onBlur",
    initialValue:"",
    rules: [{
        required:true,
       message:"請輸入"
   }, {
        validator: this.ip_reg_edit_end.bind(this)
   }]
   })(<Input key="6" type="text" onBlur={this.blurIPEditEnd.bind(this)}/>)}
</FormItem>

ip_reg_edit_end(rule, value, callback){
        var reg=/^((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))$/
        if (!value) {
            callback()    //   必須每一個語句中最后都要有callback語句,不然會一直轉圈
}else if(!reg.test(value)){ callback("請輸入正確格式的IP")    // 必須每一個語句中最后都要有callback語句,不然會一直轉圈
}else{ callback()   // 必須每一個語句中最后都要有callback語句,不然會一直轉圈 } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM