字数判断

steryzone 2008-06-04 06:54:08
现在我想做一个判断,下面代码

选择字数类型
<input type="radio" name="type" value="1"/>0~1000字
<input type="radio" name="type" value="2"/>1000~3000字
<input type="radio" name="type" value="3"/>3000~4000字
<input type="radio" name="type" value="4"/>4000~5000字

填写您字数<input type="text" name="currency"/>
<input type="submit" name="submit" value="submit"/>

怎样当他填上的数字不是所选的类型时发出警告
...全文
122 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouzongxi 2008-06-04
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档 </title>
</head>
<script>
function fun(){
var a=document.form1.currency.value;


if(document.form1.type[0].checked)//判断是否选中 type[0]表示第1个
{
if(a.length <1000 &&a.length>0)//判断范围
{
alert("yes");
}
else {
alert("字数不在这个范围");
}
}
}
</script>
<body>
<form name="form1">选择字数类型 <!--form表单-->
<input type="radio" name="type" value="1"/>0~1000字
<input type="radio" name="type" value="2"/>1000~3000字
<input type="radio" name="type" value="3"/>3000~4000字
<input type="radio" name="type" value="4"/>4000~5000字

填写您字数 <input type="text" name="currency" />
<input type="button" name="submit" value="submit" onclick="fun()"/> <!--type改为button onclick调用javascript-->

</form>
</body>
</html>

我没写没选中的情况,你操作的时候先选中第1个按钮,在文本里面输入数据 然后点按钮,不可能没反应的 我才做的 不行给源文件你了
steryzone 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 radeonxhl 的回复:]
JScript code
function fun(){
var a=document.form1.currency.value;
var aType = document.form1.type;
var bValid = false;
for(var i=0; i<aType.length; i++){
if(aType[i].checked == true){
switch(i){
case 0:
bValid = a>0&&a<1000;
break;
case 1:
bValid = a>1000&&…
[/Quote]
你这个怎样都会报ok的。。。不会报错的
radeonxhl 2008-06-04
  • 打赏
  • 举报
回复

function fun(){
var a=document.form1.currency.value;
var aType = document.form1.type;
var bValid = false;
for(var i=0; i<aType.length; i++){
if(aType[i].checked == true){
switch(i){
case 0:
bValid = a>0&&a<1000;
break;
case 1:
bValid = a>1000&&a<3000;
break;
case 2:
bValid = a>3000&&a<4000;
break;
case 3:
bValid = a>4000&&a<5000;
}
}
}
if(bValid){
alert("OK");
} else {
alert("NG");
}
}


把上面的js改了一下,呵呵
steryzone 2008-06-04
  • 打赏
  • 举报
回复
好像没有反应。。。
zhouzongxi 2008-06-04
  • 打赏
  • 举报
回复
[code=JS<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script>
function fun(){
var a=document.form1.currency.value;


if(document.form1.type[0].checked)//判断是否选中 type[0]表示第1个
{
if(a.length<1000 &&a.length>0)//判断范围
{
alert("yes");
}
else {
alert("字数不在这个范围");
}
}
}
</script>
<body>
<form name="form1">选择字数类型<!--form表单-->
<input type="radio" name="type" value="1"/>0~1000字
<input type="radio" name="type" value="2"/>1000~3000字
<input type="radio" name="type" value="3"/>3000~4000字
<input type="radio" name="type" value="4"/>4000~5000字

填写您字数<input type="text" name="currency" />
<input type="button" name="submit" value="submit" onclick="fun()"/> <!--type改为button onclick调用javascript-->

</form>
</body>
</html>
cript][/code]

其他的一样
zhouzongxi 2008-06-04
  • 打赏
  • 举报
回复
代码过会贴出来 在写。。我也学js
zhouzongxi 2008-06-04
  • 打赏
  • 举报
回复
先把text的字数选出来
然后和你选中的比较范围,不在范围就报错
zzr82528 2008-06-04
  • 打赏
  • 举报
回复
要用js来写,响应input,在onchange方法写回调,当输入焦点离来输入框时就会自动执行。

要想在他输入的时候就检查,也可以,自已稍稍研究一下,以前写过一个时间的输入框,能做到即时检验,我已经记不得具体实现了。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧