Js简单判断四则运算是否可用
function
?test(s){?
????
try
{
????????
return
?!isNaN(eval(
"("
+s+
")"
));
????
}
catch
(e){
????????
return
?false
;
????
}
}
alert(test(
"1+6*(2+(3-1)/1+(9-5*1))"
));
//--------------------------------------------------------------------------------------<script>
var
?str?=?
"1+6*(2+(3-1)/1+(9-5*1))"
;
try
{
????
eval(
"var?ret?=?"
+str);
????
if
(ret!=Infinity){
????????
alert(
"right"
);
????
}
else
{
????????
alert(
"wrong"
);
????
}
}
catch
(e){
????
alert(
"wrong"
);
}
</script>
//--------------------------------------------------------------------------------------function
?test(s){
????
var
?reg=/[\d\.\+\-\*\/\(\)]+/;
????
try
{
????????
return
?ret.test(s)&&!isNaN(eval(
"("
+s+
")"
));
????
}
catch
(e){
????????
return
?false
;
????
}
}
alert(test(
"1+6*(2+(3-1)/1+(9-5*1))"
));
//--------------------------------------------------------------------------------------<script>
var
?s?=?
'1+6*(2+(3-1)/1+(9-5*1))'
;
if
(s.match(/[^\+\-\*\/0-9()]/))
{
????
alert(
'illegal!'
)
}
else
{
????
try
????
{
????????
alert(eval(s));
????
}
????
catch
(e)
????
{
????????
alert(
'illegal'
)
????
}
}
</script>