Reputation: 51
I want to validate the format of a social security number when initializing an object or using a setter to change the value of that object after its been made. Obviously this will be done via an if statement and a regex string but I do not know how to make that happen in Java.
I found this expression on a different site:
Regex : ^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$
However i cannot tell if its accurate or not, nor do I understand the syntax to check user input against this.
setSocNum(String x){
if (![regex here]){
System.out.println("please enter a valid social security number")
}
}
I am unable to tell how to check against a regex string. If Someone can help me with the syntax here I could easily adapt it to other parts of the class I am working on. Thanks!
Upvotes: 0
Views: 4600
Reputation: 18357
You can use Pattern class from java and use its matches
method which takes two arguments, first one being the regex that you have mentioned and second argument a string and it returns a boolean
value which will tell whether the string matched with the regex or not. Here is a sample code for same,
String input = "somedata";
Pattern.matches("^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$", input);
Your method should look something like this,
public void setSocNum(String x) {
if (Pattern.matches("^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$", x)) {
System.out.println("please enter a valid social security number");
}
// further logic goes here
}
Creation of Pattern
object is heavy, so if you intend to use the code where it gets called many times, you should create it once and re-use and avoid re-creation of it again and again.
Here is the explanation of regex:
^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$
^
- Start of string(?!000|666)
- This negative look ahead means, fail the match if the string starts with 000
or 666
[0-8]
- Then match and consume the next digit if it is in range 0
to 8
[0-9]{2}
- Then match and consume the next any two digits where [0-9] means any digit which you can also compactly write as \d
-
- Then followed by a hyphen(?!00)
- This negative look ahead rejects the match if the next two digits are 00
[0-9]{2}
- Then next it matches and consumes any two digits-
- Then again followed by a hyphen(?!0000)
- This negative look ahead rejects the match if the next four digits are 0000
[0-9]{4}
- Then next it matches and consumes any four digits$
- End of stringHope my explanation is clear. Let me know for any queries.
Here is the code I suggested in my comment,
public static Pattern SOCIAL_SECURITY_NUM_PAT = Pattern.compile("^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$");
public static boolean isValidSocialSecurityNumber(String input) {
return SOCIAL_SECURITY_NUM_PAT.matcher(input).matches();
}
You can place this code in some class like SocialSecurityUtils.java
and call it from wherever you want using SocialSecurityUtils.isValidSocialSecurityNumber("socialsecuritynumber")
Upvotes: 4
Reputation: 765
Here's the meaning of this pattern: ^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$
(?!000|666)
: Look ahead in the provided string and ensures that it does not have 000
or 666
. If you find those patterns, then fail the match - return invalid input.
[0-8][0-9]{2}-
: Expects three digits followed by a -
where the first one is from a range of 0 to 8, and the other two are from a range of 0 to 9
(?!0000)
Similar to the first one, look ahead and make sure that this pattern 0000
does not exist otherwise fail the validation
[0-9]{4}
: Expects four digits from a range of 0 to 9
As for ^
and $
, they match the beginning and the end of the string respectively.
Hope that helps.
Upvotes: 2
Reputation: 77
You can use a Pattern
to match the string with regex.
private static Pattern socSecPattern = Pattern.compile("^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$");
private void setSocNum(String x){
if (!socSecPattern.matcher(x).matches()){
System.out.println("please enter a valid social security number")
}
}
It is a good idea to have the Pattern
variable as be static since it will be called many times.
Upvotes: 2