How can I bean-validate on a char?










0















I just found that @Pattern might work (only) with CharSequences.



How can I validate a single char?



//@Pattern(regex = "[YN]") // @@?
private char someYn; // 'Y' or 'N'


Will it blend?



Using an AttributeConverter is not an option.



Oh, is a single char also a CharSequence?










share|improve this question

















  • 1





    @Pattern(regexp = "^[Y|N]1$", message ="Must be Y or N") try this.You cannot use @Pattern for Character variable, You will get exception

    – GauravRai1512
    Nov 14 '18 at 1:31












  • @GauravRai1512 Is 1 part necessary? I'm not asking about the regex, anyway, thanks.

    – Jin Kwon
    Nov 14 '18 at 1:36












  • Yes, 1 is necessary to match any character.

    – GauravRai1512
    Nov 14 '18 at 1:39






  • 1





    @GauravRai1512 That is not true. 1 is completely redundant.

    – VGR
    Nov 14 '18 at 1:43















0















I just found that @Pattern might work (only) with CharSequences.



How can I validate a single char?



//@Pattern(regex = "[YN]") // @@?
private char someYn; // 'Y' or 'N'


Will it blend?



Using an AttributeConverter is not an option.



Oh, is a single char also a CharSequence?










share|improve this question

















  • 1





    @Pattern(regexp = "^[Y|N]1$", message ="Must be Y or N") try this.You cannot use @Pattern for Character variable, You will get exception

    – GauravRai1512
    Nov 14 '18 at 1:31












  • @GauravRai1512 Is 1 part necessary? I'm not asking about the regex, anyway, thanks.

    – Jin Kwon
    Nov 14 '18 at 1:36












  • Yes, 1 is necessary to match any character.

    – GauravRai1512
    Nov 14 '18 at 1:39






  • 1





    @GauravRai1512 That is not true. 1 is completely redundant.

    – VGR
    Nov 14 '18 at 1:43













0












0








0








I just found that @Pattern might work (only) with CharSequences.



How can I validate a single char?



//@Pattern(regex = "[YN]") // @@?
private char someYn; // 'Y' or 'N'


Will it blend?



Using an AttributeConverter is not an option.



Oh, is a single char also a CharSequence?










share|improve this question














I just found that @Pattern might work (only) with CharSequences.



How can I validate a single char?



//@Pattern(regex = "[YN]") // @@?
private char someYn; // 'Y' or 'N'


Will it blend?



Using an AttributeConverter is not an option.



Oh, is a single char also a CharSequence?







java bean-validation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 1:26









Jin KwonJin Kwon

10k666106




10k666106







  • 1





    @Pattern(regexp = "^[Y|N]1$", message ="Must be Y or N") try this.You cannot use @Pattern for Character variable, You will get exception

    – GauravRai1512
    Nov 14 '18 at 1:31












  • @GauravRai1512 Is 1 part necessary? I'm not asking about the regex, anyway, thanks.

    – Jin Kwon
    Nov 14 '18 at 1:36












  • Yes, 1 is necessary to match any character.

    – GauravRai1512
    Nov 14 '18 at 1:39






  • 1





    @GauravRai1512 That is not true. 1 is completely redundant.

    – VGR
    Nov 14 '18 at 1:43












  • 1





    @Pattern(regexp = "^[Y|N]1$", message ="Must be Y or N") try this.You cannot use @Pattern for Character variable, You will get exception

    – GauravRai1512
    Nov 14 '18 at 1:31












  • @GauravRai1512 Is 1 part necessary? I'm not asking about the regex, anyway, thanks.

    – Jin Kwon
    Nov 14 '18 at 1:36












  • Yes, 1 is necessary to match any character.

    – GauravRai1512
    Nov 14 '18 at 1:39






  • 1





    @GauravRai1512 That is not true. 1 is completely redundant.

    – VGR
    Nov 14 '18 at 1:43







1




1





@Pattern(regexp = "^[Y|N]1$", message ="Must be Y or N") try this.You cannot use @Pattern for Character variable, You will get exception

– GauravRai1512
Nov 14 '18 at 1:31






@Pattern(regexp = "^[Y|N]1$", message ="Must be Y or N") try this.You cannot use @Pattern for Character variable, You will get exception

– GauravRai1512
Nov 14 '18 at 1:31














@GauravRai1512 Is 1 part necessary? I'm not asking about the regex, anyway, thanks.

– Jin Kwon
Nov 14 '18 at 1:36






@GauravRai1512 Is 1 part necessary? I'm not asking about the regex, anyway, thanks.

– Jin Kwon
Nov 14 '18 at 1:36














Yes, 1 is necessary to match any character.

– GauravRai1512
Nov 14 '18 at 1:39





Yes, 1 is necessary to match any character.

– GauravRai1512
Nov 14 '18 at 1:39




1




1





@GauravRai1512 That is not true. 1 is completely redundant.

– VGR
Nov 14 '18 at 1:43





@GauravRai1512 That is not true. 1 is completely redundant.

– VGR
Nov 14 '18 at 1:43












1 Answer
1






active

oldest

votes


















1














You can write your own ConstraintValidator:



https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html



Where you can provide annotation and validation type
for which you can define your logic
And then you can annotate your field with your custom annotation



Here is link with steps how to make it :



https://dzone.com/articles/create-your-own-constraint-with-bean-validation-20



As per javax validation documentation :



@Pattern(regex=, flag=) String.



Additionally supported by HV: any sub-type of CharSequence.



Checks if the annotated string matches the regular expression regex considering the given flag



So actually with Character using @Pattern you will get the error



Thanks






share|improve this answer

























  • Sir, request to confirm, @Pattern doesn't work for char? Thanks.

    – Jin Kwon
    Nov 14 '18 at 1:46











  • You can refer to post stackoverflow.com/questions/12599069/…

    – Mykhailo Moskura
    Nov 14 '18 at 1:53











  • Updated answer thanks

    – Mykhailo Moskura
    Nov 14 '18 at 1:59






  • 1





    Yes as per javax.validation says we cannot use Character

    – Mykhailo Moskura
    Nov 14 '18 at 2:04










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291869%2fhow-can-i-bean-validate-on-a-char%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can write your own ConstraintValidator:



https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html



Where you can provide annotation and validation type
for which you can define your logic
And then you can annotate your field with your custom annotation



Here is link with steps how to make it :



https://dzone.com/articles/create-your-own-constraint-with-bean-validation-20



As per javax validation documentation :



@Pattern(regex=, flag=) String.



Additionally supported by HV: any sub-type of CharSequence.



Checks if the annotated string matches the regular expression regex considering the given flag



So actually with Character using @Pattern you will get the error



Thanks






share|improve this answer

























  • Sir, request to confirm, @Pattern doesn't work for char? Thanks.

    – Jin Kwon
    Nov 14 '18 at 1:46











  • You can refer to post stackoverflow.com/questions/12599069/…

    – Mykhailo Moskura
    Nov 14 '18 at 1:53











  • Updated answer thanks

    – Mykhailo Moskura
    Nov 14 '18 at 1:59






  • 1





    Yes as per javax.validation says we cannot use Character

    – Mykhailo Moskura
    Nov 14 '18 at 2:04















1














You can write your own ConstraintValidator:



https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html



Where you can provide annotation and validation type
for which you can define your logic
And then you can annotate your field with your custom annotation



Here is link with steps how to make it :



https://dzone.com/articles/create-your-own-constraint-with-bean-validation-20



As per javax validation documentation :



@Pattern(regex=, flag=) String.



Additionally supported by HV: any sub-type of CharSequence.



Checks if the annotated string matches the regular expression regex considering the given flag



So actually with Character using @Pattern you will get the error



Thanks






share|improve this answer

























  • Sir, request to confirm, @Pattern doesn't work for char? Thanks.

    – Jin Kwon
    Nov 14 '18 at 1:46











  • You can refer to post stackoverflow.com/questions/12599069/…

    – Mykhailo Moskura
    Nov 14 '18 at 1:53











  • Updated answer thanks

    – Mykhailo Moskura
    Nov 14 '18 at 1:59






  • 1





    Yes as per javax.validation says we cannot use Character

    – Mykhailo Moskura
    Nov 14 '18 at 2:04













1












1








1







You can write your own ConstraintValidator:



https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html



Where you can provide annotation and validation type
for which you can define your logic
And then you can annotate your field with your custom annotation



Here is link with steps how to make it :



https://dzone.com/articles/create-your-own-constraint-with-bean-validation-20



As per javax validation documentation :



@Pattern(regex=, flag=) String.



Additionally supported by HV: any sub-type of CharSequence.



Checks if the annotated string matches the regular expression regex considering the given flag



So actually with Character using @Pattern you will get the error



Thanks






share|improve this answer















You can write your own ConstraintValidator:



https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html



Where you can provide annotation and validation type
for which you can define your logic
And then you can annotate your field with your custom annotation



Here is link with steps how to make it :



https://dzone.com/articles/create-your-own-constraint-with-bean-validation-20



As per javax validation documentation :



@Pattern(regex=, flag=) String.



Additionally supported by HV: any sub-type of CharSequence.



Checks if the annotated string matches the regular expression regex considering the given flag



So actually with Character using @Pattern you will get the error



Thanks







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 2:05

























answered Nov 14 '18 at 1:42









Mykhailo MoskuraMykhailo Moskura

838113




838113












  • Sir, request to confirm, @Pattern doesn't work for char? Thanks.

    – Jin Kwon
    Nov 14 '18 at 1:46











  • You can refer to post stackoverflow.com/questions/12599069/…

    – Mykhailo Moskura
    Nov 14 '18 at 1:53











  • Updated answer thanks

    – Mykhailo Moskura
    Nov 14 '18 at 1:59






  • 1





    Yes as per javax.validation says we cannot use Character

    – Mykhailo Moskura
    Nov 14 '18 at 2:04

















  • Sir, request to confirm, @Pattern doesn't work for char? Thanks.

    – Jin Kwon
    Nov 14 '18 at 1:46











  • You can refer to post stackoverflow.com/questions/12599069/…

    – Mykhailo Moskura
    Nov 14 '18 at 1:53











  • Updated answer thanks

    – Mykhailo Moskura
    Nov 14 '18 at 1:59






  • 1





    Yes as per javax.validation says we cannot use Character

    – Mykhailo Moskura
    Nov 14 '18 at 2:04
















Sir, request to confirm, @Pattern doesn't work for char? Thanks.

– Jin Kwon
Nov 14 '18 at 1:46





Sir, request to confirm, @Pattern doesn't work for char? Thanks.

– Jin Kwon
Nov 14 '18 at 1:46













You can refer to post stackoverflow.com/questions/12599069/…

– Mykhailo Moskura
Nov 14 '18 at 1:53





You can refer to post stackoverflow.com/questions/12599069/…

– Mykhailo Moskura
Nov 14 '18 at 1:53













Updated answer thanks

– Mykhailo Moskura
Nov 14 '18 at 1:59





Updated answer thanks

– Mykhailo Moskura
Nov 14 '18 at 1:59




1




1





Yes as per javax.validation says we cannot use Character

– Mykhailo Moskura
Nov 14 '18 at 2:04





Yes as per javax.validation says we cannot use Character

– Mykhailo Moskura
Nov 14 '18 at 2:04

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291869%2fhow-can-i-bean-validate-on-a-char%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3