Skip to main content
Participant
June 1, 2021
Answered

custom format with 2 possible conditions

  • June 1, 2021
  • 1 reply
  • 1005 views

I am tring to set a text cell to only allow 2 posible formats , so only 3 munmbers like 123 or AA234 so eather 5 caracters starting with 2 letters or only 3 characters but it can be one or the other. 

 

So I tried using regex and some if conditions, but I need help to make it work or find an easier solution. 

I added the following code in the custom keystroke script with custom  Format area. 

any feedback on where to put it and if it is correct? or should I be using AA999 and 999 instread of the rejex native rules? 

var myRegExp2 = \D{2}\d{3};
var myRegExp = \d{3};
var myTextInput = event.value;
event.rc = true;
if((myRegExp.test(myTextInput)) || (myRegExp2.test(myTextInput))){event.target.textColor = color.black;} else{ app.alert( "You need to type 9 characters or a ISQ 12 characters starting by2 letters ")}

This topic has been closed for replies.
Correct answer try67

As I wrote, you should use it as the Validation script, not Format.

1 reply

try67
Community Expert
Community Expert
June 1, 2021

This should be a validation script, and you need to adjust your regular expressions (which are not correctly set up, by the way) to match the entire string, not just a part of it. If you want you can also reject the value the user entered if it's invalid. Try this code:

 

var myRegExp2 = /^[a-z]{2}\d{3}$/;
var myRegExp = /^\d{3}$/;
var myTextInput = event.value;
if (myTextInput && myRegExp.test(myTextInput)==false && myRegExp2.test(myTextInput)==false) {
	app.alert( "You need to type 9 characters or a ISQ 12 characters starting by 2 letters ");
	event.rc = false;
}

 

PS. Your error message doesn't seem to match the values you want to accept... You might want to review it.

Participant
June 1, 2021

I apreciate you pointing out the discrepancy, yet  .. the comment is not important that can be changed, what I need to find is basically how to make adobe ask for 2 different format positibilities and not just random things. is easy to ask for one or the other alone but, to include the "OR" is what is not working or in any case the code is not doing anithing when I palce it in the special formating.  So if people want to add any one of the "2"  depending on the case.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 1, 2021

As I wrote, you should use it as the Validation script, not Format.