Hi,
I am looking for a solution where in if the user types the phone number '1234567890' in the text box custom field and presses tab, it should change it to '123/456/7890' format with the regex validation in place.
Any idea or solutions how we can implement this? Any plugins that can be used? Also, it would be greatly helpful if anyone can suggest the regex validator expression as well that can be used in Adaptavist ScriptRunner behaviors.
Thanks,
Vamsi
Hi,
For those who are looking to implementing this solution:
This was implemented using 'ScriptRunner' behaviors.
Add the following script to the relevant phone number field.
def mobile_number = getFieldById("customfield_12345");
def pattern = /^\d{3}\/\d{3}\/\d{4}$/
def error_message = "Not a valid phone number. It should be 10 digits or in the proper format e.g. 123/456/7890"
mobile_number.setDescription("Phone number should be 10 digits with format as 123/456/7890.");
if (mobile_number.getValue().toString().length() != 10) {
mobile_number.setError("Phone number should be 10 digits. Please check and enter correct value.")
}
else if (mobile_number.getValue().toString().length() == 10)
{
def area_code = mobile_number.getValue().toString().substring(0,3);
def phone_number_1 = mobile_number.getValue().toString().substring(3, 6);
def phone_number_2 = mobile_number.getValue().toString().substring(6, 10);
def new_phone_number = area_code + "/" + phone_number_1 + "/" + phone_number_2;
mobile_number.setFormValue(new_phone_number.toString());
mobile_number.setError("");
}
if (mobile_number.getValue()) {
def matcher = mobile_number.getValue() =~ pattern
if (!matcher.size()) {
mobile_number.setError(error_message);
}
else {
mobile_number.setError("");
}
}
else {
mobile_number.setError("");
}
Hope this helps!
Thanks,
Vamsi
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.