...

Source file src/gitlab.hexacode.org/go-libs/microservice/validate/validate_email.go

Documentation: gitlab.hexacode.org/go-libs/microservice/validate

     1  /*
     2   * Micro Service
     3   * Package: gitlab.hexacode.org/go-libs/microservice
     4   * Maintainer: Azzis Arswendo <azzis@hexacode.org>
     5   *
     6   * Copyright (C) 2023 Hexacode Teknologi Indonesia
     7   * All Rights Reserved
     8   */
     9  
    10  package validate
    11  
    12  import (
    13  	"fmt"
    14  	"regexp"
    15  )
    16  
    17  func (options *Options) ValidEmail() error {
    18  	if !options.Required && options.ValueText == "" {
    19  		return nil
    20  	}
    21  
    22  	regexPattern := "^[a-zA-Z0-9.-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
    23  
    24  	regex, err := regexp.Compile(regexPattern)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	isValid := regex.MatchString(options.ValueText)
    30  	if !isValid {
    31  		return fmt.Errorf("invalid type email: Email must be valid")
    32  	}
    33  
    34  	return nil
    35  }
    36  

View as plain text