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>
相关推荐
zhiliang 2020-01-31
roseying 2019-12-01
星愿 2019-11-16
85397518 2019-11-16
getianao 2019-11-16
yboker 2019-06-30
hyv 2014-01-13
袁萌 2017-10-01
xiaohao 2011-04-06
drilistbox 2019-04-25
pythoncream 2016-02-27
LHpython 2019-04-08
吴家应 2014-03-10
PHP100 2019-03-28
PHP100 2019-03-27
明学的白板 2018-05-20
锦妖和她的小伙伴们 2018-04-19