github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_password3.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  // RulePassword3 implements `password3` rule:
    16  // Universal password format rule3:
    17  // Must meet password rule1, must contain lower and upper letters, numbers and special chars.
    18  //
    19  // Format: password3
    20  type RulePassword3 struct{}
    21  
    22  func init() {
    23  	Register(RulePassword3{})
    24  }
    25  
    26  func (r RulePassword3) Name() string {
    27  	return "password3"
    28  }
    29  
    30  func (r RulePassword3) Message() string {
    31  	return "The {field} value `{value}` is not a valid password3 format"
    32  }
    33  
    34  func (r RulePassword3) Run(in RunInput) error {
    35  	var value = in.Value.String()
    36  	if gregex.IsMatchString(`^[\w\S]{6,18}$`, value) &&
    37  		gregex.IsMatchString(`[a-z]+`, value) &&
    38  		gregex.IsMatchString(`[A-Z]+`, value) &&
    39  		gregex.IsMatchString(`\d+`, value) &&
    40  		gregex.IsMatchString(`[^a-zA-Z0-9]+`, value) {
    41  		return nil
    42  	}
    43  	return errors.New(in.Message)
    44  }