github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_phone.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/wangyougui/gf.
     6  
     7  package builtin
     8  
     9  import (
    10  	"errors"
    11  
    12  	"github.com/wangyougui/gf/v2/text/gregex"
    13  )
    14  
    15  // RulePhone implements `phone` rule:
    16  //
    17  //  1. China Mobile:
    18  //     134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188,
    19  //     178(4G), 147(Net);
    20  //     172
    21  //
    22  //  2. China Unicom:
    23  //     130, 131, 132, 155, 156, 185, 186 ,176(4G), 145(Net), 175
    24  //
    25  //  3. China Telecom:
    26  //     133, 153, 180, 181, 189, 177(4G)
    27  //
    28  //  4. Satelite:
    29  //     1349
    30  //
    31  //  5. Virtual:
    32  //     170, 173
    33  //
    34  //  6. 2018:
    35  //     16x, 19x
    36  //
    37  // Format: phone
    38  type RulePhone struct{}
    39  
    40  func init() {
    41  	Register(RulePhone{})
    42  }
    43  
    44  func (r RulePhone) Name() string {
    45  	return "phone"
    46  }
    47  
    48  func (r RulePhone) Message() string {
    49  	return "The {field} value `{value}` is not a valid phone number"
    50  }
    51  
    52  func (r RulePhone) Run(in RunInput) error {
    53  	ok := gregex.IsMatchString(
    54  		`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[\d]{9}$|^17[0,2,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[\d]{9}$`,
    55  		in.Value.String(),
    56  	)
    57  	if ok {
    58  		return nil
    59  	}
    60  	return errors.New(in.Message)
    61  }