 ShowAll Questionssorted byDate Posted lincy

# Formula is not working one part regex is working

Hi Can any one help me,
I have a text field in which i am trying to save alpha numeric and i am trying to limit the range between 0100 to 0555 so the values should save between the range and alpha numeric.i worte a formula like this.
AND(
NOT(REGEX(Text__c,"^[a-zA-Z0-9_]*\$")),
OR(
VALUE(Text__c) < 0100,
VALUE(Text__c) > 0555 ) )

So here example am giving AC15 value in the field it is working.
if i give the value 0101 it is saving and if i give it as 1111 it is
also saving which is not correct.REGEX part is working but not the other part  OR Part. Best Answer chosen by lincy Alain Cabon
Try the code below with the developer console + anonymous window (CTRL+E); all the values between '0100' and '0555' included work;

```Pattern r = Pattern.compile('^0([1-4][0-9][0-9]|5([0-4][0-9]|5[0-5]))\$');
String[] st = new String[]{'AC15','0099','0100','0555','0499','0400','0500','0556','1000','A100'};
system.debug('st:' +st.size());
for (integer i=0;i<st.size();i++) {
Matcher rm = r.matcher(st[i]);
if(rm.matches()) {
system.debug(st[i] + ' found');
} else {
}
}
// test all the values between 0100 and 0999
for (integer i=100;i<=999;i++) {
Matcher rm = r.matcher('0'+i);
if(rm.matches()) {
system.debug('0' + i + ' found');
} else {
system.debug('0' + i + ' not found');
}
}
```

There is no value that doesn't work.

You cannot find a value that doesn't work. Alain Cabon
You should treat each figure of the whole number.by range of numbers.

Always zero at the beginning.

0  - 100  - 499
0  - 500  - 549 - 555
```Pattern r = Pattern.compile('^0([1-4][0-9][0-9]|5([0-4][0-9]|5[0-5]))\$');
String[] st = new String[]{'0099','0100','0555','0499','0400','0500','0556','1100','A100'};
system.debug('st:' +st.size());
for (integer i=0;i<st.size();i++) {
Matcher rm = r.matcher(st[i]);
if(rm.find()) {
system.debug(st[i] + ' found');
} else {
}
}
``` Alain Cabon

if(rm.matches())  is better for a test here.

```Pattern r = Pattern.compile('^0([1-4][0-9][0-9]|5([0-4][0-9]|5[0-5]))\$');
String[] st = new String[]{'0099','0100','0555','0499','0400','0500','0556','1100','A100'};
system.debug('st:' +st.size());
for (integer i=0;i<st.size();i++) {
Matcher rm = r.matcher(st[i]);
if(rm.matches()) {
system.debug(st[i] + ' found');
} else {
}
}``` lincy
cant we do that in a validation rule. Alain Cabon

REGEX(Text__c,"^0([1-4][0-9][0-9]|5([0-4][0-9]|5[0-5]))\$")

https://developer.salesforce.com/forums/?id=9060G0000005VNCQA2 lincy
still the regex is not working can any one help Alain Cabon
Try the code below with the developer console + anonymous window (CTRL+E); all the values between '0100' and '0555' included work;

```Pattern r = Pattern.compile('^0([1-4][0-9][0-9]|5([0-4][0-9]|5[0-5]))\$');
String[] st = new String[]{'AC15','0099','0100','0555','0499','0400','0500','0556','1000','A100'};
system.debug('st:' +st.size());
for (integer i=0;i<st.size();i++) {
Matcher rm = r.matcher(st[i]);
if(rm.matches()) {
system.debug(st[i] + ' found');
} else {
}
}
// test all the values between 0100 and 0999
for (integer i=100;i<=999;i++) {
Matcher rm = r.matcher('0'+i);
if(rm.matches()) {
system.debug('0' + i + ' found');
} else {
system.debug('0' + i + ' not found');
}
}
```

There is no value that doesn't work.

You cannot find a value that doesn't work.

This was selected as the best answer Alain Cabon
Did you try the code above?

Open the Developer Console:
https://help.salesforce.com/articleView?id=code_dev_console_opening.htm&type=5 (https://help.salesforce.com/articleView?id=code_dev_console_opening.htm&type=5)