Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6648765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:41:38+00:00 2026-05-26T00:41:38+00:00

I want to validate the trimmed string length, like: @Size(min = 5, max =

  • 0

I want to validate the trimmed string length, like:

@Size(min = 5, max = 20, message = "Please enter a valid username (5-20 characters)")
String userName;

Given user name foo (6 chars), it will be trimmed to foo (3 chars) in the setter method, however, validate against the raw input seems a bit of useless.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:41:38+00:00Added an answer on May 26, 2026 at 12:41 am

    Instead of altering the validation procedure, you could write your own constraint which wraps the expected validation logic.

    I have found such an implementation of a @TrimmedSize annotation at the auto-trader-spring-spike project:

    package com.autotrader.frameworks;
    
    import javax.validation.Constraint;
    import javax.validation.Payload;
    import java.lang.annotation.Documented;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import static java.lang.annotation.ElementType.*;
    
    @Documented
    @Constraint(validatedBy = TrimmedSizeValidator.class)
    @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TrimmedSize {
        String message() default "size too small";
    
        Class<?>[] groups() default {};
    
        int min() default 0;
    
        Class<? extends Payload>[] payload() default {};
    }
    

    And the validator:

    package com.autotrader.frameworks;
    
    
    import javax.validation.ConstraintValidator;
    import javax.validation.ConstraintValidatorContext;
    
    public class TrimmedSizeValidator implements ConstraintValidator<TrimmedSize, String> {
    
        private int length;
    
        public void initialize(TrimmedSize trimmedSize) {
            length = trimmedSize.min();
        }
    
        public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
            if (value == null || value.length() == 0) {
                return false;
            }
            return value.trim().length() >= length;
        }
    }
    

    It uses an Apache License 2.0, but even then you could write this code from scratch once you’ve seen it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to validate length and restrict Numeric, alphanumeric, and alpha characters only in
I want to validate a string only if it contains '0-9' chars with length
I want to validate a texbox with min char=5, max=20, allow alphabet and numbers
I want to validate a string which donot have numeric characters. If my string
I want to validate a set of credentials against the domain controller. e.g.: Username:
I have some MET data I want to validate which would look something like
I want to validate a text box that doesn't accept any special characters using
I want to validate a string against a set of values using annotations. What
I want validate HTML code in Java. Like this: HTMLValidator.validateHTML(aaa<b>bbb<b>); // Returns false, because
I want to validate any string that contains çÇöÖİşŞüÜğĞ chars and starting at least

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.