简体   繁体   English

Java Bean验证和正则表达式-格式和长度中的两种不同的错误消息

[英]Java Bean validation and regex - format and length in two different error message

For example, input is phone number. 例如,输入为电话号码。 Format is 格式为

(country code)(space)(area code)(space)(phone number) (国家代码)(空格)(区域代码)(空格)(电话号码)

Area code can be blank. 区号可以为空白。

e.g.
63 240 1234567   (valid)
63 1234567  (valid)

What regex pattern do I need so I can have it check if all characters are numeric and if the phone number (without country and area code) is of a certain length? 我需要哪种正则表达式模式,以便可以检查所有字符是否都是数字,以及电话号码(不含国家和地区代码)是否具有一定长度?

and is it possible that using only one pattern, I can have two distinct error messages for two distinct invalid input: 1. All characters should be numeric 2. Actual Phone number length should be 7 to 10. 并且有可能仅使用一种模式,对于两个不同的无效输入,我可能会有两个不同的错误消息:1.所有字符应为数字2.实际电话号码长度应为7到10。

because we currently have it this way 因为我们目前有这种方式

@Pattern(regexp=" some regex pattern", message="phone is invalid")
private String phoneNumber

I could add a @Size validator but I only need to trim out the phone number. 我可以添加一个@Size验证器,但是我只需要修剪电话号码。

Can anyone help? 有人可以帮忙吗?

This expression could work: 该表达式可以工作:

^(\d{2})\s(?:(\d{3})\s)?(\d{7,10})$

Explanation: 说明:

  • ^ Begining of string ^字符串开头
  • (\\d{2})\\s 2 digits, followed by a space (country code)(space) (\\d{2})\\s 2位数字,后跟一个空格(国家代码)(空格)
  • (?:(\\d{3})\\s)? (optional) 3 digits, followed by a space (area code)(space) (可选)3位数字,后跟一个空格(区号)(空格)
  • (\\d{7,10}) 7 to 10 digits (\\d{7,10}) 7到10位数字
  • $ End of string $字符串结尾

Example

Limitations: 局限性:

  • It will match things like 63 240 1234567 and 63 1234567 but it wouldn't match 631234567 or 63 240 1234567 , it's expecting a space after each of those sections. 它将匹配63 240 123456763 1234567类的东西,但不匹配63123456763 240 1234567 ,它期望每个部分之后都有一个空格。 It's easy to work around those limitations, but it depends on the level of validation you require. 解决这些限制很容易,但这取决于所需的验证级别。 If spaces are optional, you could replace both \\s by \\s? 如果空格是可选的,则可以将两个\\s替换为\\s?

Variant 变体

If you want all the spaces and the country code to be optional, here's a variant: ^(?:(\\d{2})\\s?)?(?:(\\d{3})\\s?)?(\\d{7,10})$ 如果您希望所有空格和国家/地区代码为可选,请使用以下变体: ^(?:(\\d{2})\\s?)?(?:(\\d{3})\\s?)?(\\d{7,10})$

Code Sample 代码样例

If you would like to evaluate both patterns, in sequential order, you could go with this answer ( https://stackoverflow.com/a/41603564/7400458 ), for the Java code, combined with the two regex above. 如果您希望按顺序评估这两种模式,则可以使用以下答案( https://stackoverflow.com/a/41603564/7400458 ),针对Java代码,并结合上面的两个正则表达式。

 @GroupSequence({First.class, Second.class}) public interface Sequence {} @Size(min = 2, max = 10, message = "Name length improper", groups = { First.class }) @Pattern(regexp = "T.*", message = "Name doesn't start with T" , groups = { Second.class }) private String name; 

When now validating a Bean instance using the defined sequence (validator.validate(bean, Sequence.class)), at first the @Size constraint will be validated and only if that succeeds the @Pattern constraint. 现在,当使用定义的序列(validator.validate(bean,Sequence.class))验证Bean实例时,首先将验证@Size约束,并且前提是该约束必须成功@Pattern约束。

Something like that: 像这样:

 @GroupSequence({First.class, Second.class})
 public interface Sequence {}

 @Pattern(regexp = "^[ \d]+$", message = "Numeric input required" , groups = { First.class })
 @Pattern(regexp = "^(?:(\d{2})\s?)?(?:(\d{3})\s?)?(\d{7,10})$", message = "Phone no. should be 7-10 in length" , groups = { Second.class })
 private String phoneNumber;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM