github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_password2.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  // RulePassword2 implements `password2` rule:
    16  // Universal password format rule2:
    17  // Must meet password rule1, must contain lower and upper letters and numbers.
    18  //
    19  // Format: password2
    20  type RulePassword2 struct{}
    21  
    22  func init() {
    23  	Register(RulePassword2{})
    24  }
    25  
    26  func (r RulePassword2) Name() string {
    27  	return "password2"
    28  }
    29  
    30  func (r RulePassword2) Message() string {
    31  	return "The {field} value `{value}` is not a valid password2 format"
    32  }
    33  
    34  func (r RulePassword2) 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  		return nil
    41  	}
    42  	return errors.New(in.Message)
    43  }