Hello,
I want to have a validation for an optional field that has exactly 8 digits.
My code is:
// Import Regex
import java.util.regex.*;
// Construct The Pattern to Match on
Pattern pattern = Pattern.compile("([0-9]){8}");
// Check if the PhoneNumber Field is a match
cfValues['PhoneNumber'] == null || pattern.matcher(issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("PhoneNumber")).toString())
The problem is that allows can insert more than 8 digits.
Try this
Pattern.compile("^([0-9]{8})$")
Anna you were missing the dollar sign ($) in your snippet which means "don't add anything after me" as dollar sign means end of line.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I want to ask another case: If I need the number of digits to be between 5 to 8 digits, how I will write the command?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just make it
Pattern.compile("^([0-9]{5,8})\$")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If it works for you, please accept the answer. thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What if we want to add special characters like "-" to the above snippet? e.g 9956-56
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Your requirement of "9956-56" can be implemented with the snippet
Pattern.compile("^([-0-9]{5,8})\$")
i.e. digits with "-" (dash) and total length of 5 to 8
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.