github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_phone_loose.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package builtin
     8  
     9  import (
    10  	"errors"
    11  
    12  	"github.com/gogf/gf/v2/text/gregex"
    13  )
    14  
    15  // RulePhoneLoose implements `phone-loose` rule:
    16  // Loose mobile phone number verification(宽松的手机号验证)
    17  // As long as the 11 digits numbers beginning with
    18  // 13, 14, 15, 16, 17, 18, 19 can pass the verification
    19  // (只要满足 13、14、15、16、17、18、19开头的11位数字都可以通过验证).
    20  //
    21  // Format: phone-loose
    22  type RulePhoneLoose struct{}
    23  
    24  func init() {
    25  	Register(RulePhoneLoose{})
    26  }
    27  
    28  func (r RulePhoneLoose) Name() string {
    29  	return "phone-loose"
    30  }
    31  
    32  func (r RulePhoneLoose) Message() string {
    33  	return "The {field} value `{value}` is not a valid phone number"
    34  }
    35  
    36  func (r RulePhoneLoose) Run(in RunInput) error {
    37  	ok := gregex.IsMatchString(
    38  		`^1(3|4|5|6|7|8|9)\d{9}$`,
    39  		in.Value.String(),
    40  	)
    41  	if ok {
    42  		return nil
    43  	}
    44  	return errors.New(in.Message)
    45  }