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

  • Home
  • SEARCH
  • 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 9121999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:59:21+00:00 2026-06-17T05:59:21+00:00

I have a class: public class Foo { @Length(min = 6, max = 30)

  • 0

I have a class:

public class Foo {
    @Length(min = 6, max = 30)
    @Pattern(regexp = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*)", message = "{error.password.too_simple}")
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

As you see, there is one field ‘password’ and two validate annotations. But, in some situations, I want that only one should be applied, not both.
For example: if I have empty string: “” I want, that only one restriction be applied: the first (@Length), but in my situation applied both restrictions.
What should I do, how can I applied only one restriction in the situation?

  • 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-06-17T05:59:22+00:00Added an answer on June 17, 2026 at 5:59 am

    You could assign the constraints to different validation groups and validate them using a group sequence, e.g. by re-defining the default group sequence for Foo:

    public interface EmptyChecks {}
    
    @GroupSequence( { EmptyChecks.class, Foo.class } )
    public class Foo {
        @Length(min = 6, max = 30, groups=EmptyChecks.class)
        @Pattern(regexp = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*)", message = "{error.password.too_simple}")
        private String password;
    
        //...
    }
    

    Now the @Pattern constraint will only be validated if the @Length constraint is valid.

    Your specific case might also be solved by accepting an empty value (^$) in the regular expression of the @Password constraint:

    @Pattern(regexp = "(^$|(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*)", message = "")
    

    That way both constraints will be validated but only the @Length constraint will fail for an emtpy input value.

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

Sidebar

Related Questions

I have a class class foo { public: foo(); foo( int ); private: static
Suppose I have: class Foo { public String Bar { get; set; } }
In c# you can have public class Foo { public Foo(string name) { //do
I have a class like this public class foo { private void getThread() {
I have a class like so: [JsonObject(MemberSerialization.OptIn)] public class foo { [JsonProperty(name_in_json)] public string
If I have a class, Foo, looking like this... class Foo { public string
I have a class public class Foo{ public Foo{...} private void someFunction(){...} ... private
Let's say I have a class public class foo { public string FooId {
Suppose I have this class: class foo { public: foo() { } foo(const std::string&
Say I have some class, e.g. Foo: public class Foo { private Integer x;

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.