function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lincylincy 

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 CabonAlain 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 {
     system.debug(st[i] + ' not found'); 
   }
}
// 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.
 

All Answers

Alain CabonAlain 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 {
      system.debug(st[i] + ' not found'); 
    }
}

 
Alain CabonAlain 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 {
     system.debug(st[i] + ' not found'); 
   }
}

 
lincylincy
cant we do that in a validation rule.
Alain CabonAlain 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

 
lincylincy
still the regex is not working can any one help
Alain CabonAlain 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 {
     system.debug(st[i] + ' not found'); 
   }
}
// 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 CabonAlain 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)