I'm working on a logic where user defined multiple conditions to validate and then they also defined in what manner that condition should evaluate, understand it like this, User defined 4 different conditions and when user defined the evaluation criteria, it defined like ((1 AND 2 AND 3) OR 4)
, So I wanted to validate that evaluation criteria with the help of regex, I build following function,
function validateCustomLogic(ele){
var msg = '';
var isError = false;
var eleVal = $(ele).val();
if(typeof eleVal != 'unknown' && typeof eleVal != 'undefined' && eleVal != null && eleVal != ''){
var regEx = /^([(]*[1-9]+[ ][AND|OR]\w*[ ][1-9]*[)]*)*$/gi;
var matchEle = eleVal.match(regEx);
if(matchEle == null){
isError = true;
}
}
else{
isError = true;
}
return isError;
}
this function called on my input change where user defined the evaluation criteria,
<input onchange="validateCustomLogic(this)" type="text" />
my regex /^([(]*[1-9]+[ ][AND|OR]\w*[ ][1-9]*[)]*)*$/gi
works for some pattern identification but not for all, below is the tested result,
Input Result Expected Result
1 AND 2 AND 3 AND 4 Error: false false
1 AND 2 AND (3 AND 4) Error: false false
1 AND 2 AND 3 (AND 4) Error: true true
(1 AND 2 AND 3 AND 4 Error: false true*
)1 AND 2 AND 3 AND 4 Error: true true
(1 AND 2 AND 3) AND 4 Error: true false*
(1 AND 2)(AND 3 AND 4) Error: true true
((1 AND 2 AND 3 AND 4) Error: false true*
If you see the test result some of the entries with *
are not satisfied with this regex, not sure what I'm missing over here. Can anyone help me to validate that.