Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you make a string act as a comparison operator?

Status
Not open for further replies.

VBGuy2112

Programmer
Feb 19, 2002
58
US
This is one for a real tekie...

I am looking to dynamically create a validation function and I want to be able to pass the function a string like "==" and have it act as a comparison operator in my function.

Is there a way to do this?

Thanks in advance!!!
 
eval should interprute a string as == into the logical expression of *EQ

I'll give it a shot and see if thats sound though. just a thought _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
onpnt2.gif
[sup] [/sup]
 
hey VBGuy
I came up with this real quick but have to leave for a bit. possibly someone can get it working a bit more efficiently and more so dynamic as is. it should get you started though
<html>
<head>
<script>
function compOper(operator) {
var frm = document.forms[0];
var str1 = frm.elements[0].value;
var str2 = frm.elements[1].value;

//alert(eval(&quot;str1+operator+str2&quot;));
var expr = eval(&quot;str1+operator+str2&quot;)
if(eval(expr)) {
alert(&quot;it work, they are equal&quot;);
} else {
alert(&quot;they are not equal&quot;);

}
}
</script>
</head>

<body>
<form>
<input type=&quot;text&quot; name=&quot;str1&quot;><br>
<input type=&quot;text&quot; name=&quot;str2&quot;><br>
<input type=&quot;text&quot; name=&quot;operator&quot;><br>
<input type=&quot;button&quot; value=&quot;text comparison function&quot; onClick=&quot;compOper(document.forms[0].operator.value)&quot;>
</form>
</body>
</html>

I think it's pretty straight forward and eplains itself but if you need more explanation on what's going on in there let us know. _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
onpnt2.gif
[sup] [/sup]
 
Thank you both for your responses. I had to change onpnt's code a little to make it work but I think I have what I need. Very cool indeed.
 
Yes, well done both onpnt's [lol] Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseInt(x.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
thank you both.[smile] and both of me [lol]

I should apologize though as I was testing integers only at that point. that's probably the adjustments you needed. I always find it easier to get what I need working with integers then change over to strings in javascript. There might be a tip in there somewhere _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
onpnt2.gif
[sup] [/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top