Need an Edit Validate rule xxx-xxxxx in the format

Hi,

We have a requirement like there is a filed which is allowed only integers and the first digit should starts with “4” ,there should be a “-” after three digits and it should be in the format like 4xx-xxxxx(eg:432-87613).I was created an edit validate rule with custom java code but it was not working. So, please suggest any solution or in any possible way to achieve this.

// Regex to check valid Customer ID
String regex= “[1]{1}[0-9]{2}-?[0-9]{5}$”;

// Compile the ReGex
java.util.regex.Pattern p = java.util.regex.Pattern.compile(regex);

// If the string is empty, return false
if (theValue == null || theValue.trim().equals(“”)) return false;

// Pattern class contains matcher() method to match the given string and the regular expression
java.util.regex.Matcher m = p.matcher(theValue);

// Return if the string matched the ReGex
return m.matches();


  1. 4 ↩︎

Hi @SrinivasareddyD8393: I don’t see a issue with the code. I have tried with below code and it works for me.

if (theValue == null || theValue.trim().equals(“”)) return false;
else
{
java.util.regex.Pattern p = java.util.regex.Pattern.compile(“[1]{1}[0-9]{2}-?[0-9]{5}$”);
java.util.regex.Matcher m = p.matcher(theValue);
return m.matches();
}

Note: This validation will be fired when you save / submit the form.

Thanks


  1. 4 ↩︎

@ArulDevan Hi,

Thanks for ur response and also i need the “-” should come automatically after entering the first three digits.So, can you please suggest the possible way.

Thanks

Hi @SrinivasareddyD8393: You can add a edit input to the property as below.

if (theValue.length() > 3 && theValue.charAt(3)!=‘-’)
{
StringBuilder sb = new StringBuilder(theValue);
theValue=sb.insert(3, ‘-’).toString();

}

Thanks.

@ArulDevan Thanks , now it is working fine.

Thanks