如何在表单提交时验证test开头或结尾有无空格。诚心求解

fys249931556 2007-11-19 05:33:12
例如<form name="form1" method="post" action="http://www.csdn.net" >
<input name="id" type="text" />
<input type=submit name=button value="提交" />
</form>
如果id开头或最后一个字符是空格,则弹出对话框提示
诚心求解,希望各位帮帮小弟。不胜感激
...全文
110 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fys249931556 2007-11-21
  • 打赏
  • 举报
回复
呵呵 这次可以了。谢谢啦。。
Go 旅城通票 2007-11-20
  • 打赏
  • 举报
回复
<html>
<head></head>
<script>
function chk(f)
{
var id=f.id.value;
if(id=="")
{
alert("id不能为空!");
f.id.focus();
return false;
}
if(id.charAt(0)==" "||id.charAt(id.length-1)==" ")
{
alert("id前面或者后面不能有空格!");
f.id.focus();
return false;
}
}
</script>
<body>
<form name="form1" method="post" action= "http://www.csdn.net" onsubmit="return chk(this)">
<input name="id" type="text" />
<input type=submit name=button value="提交" />
</form>

</body>
</html>
fys249931556 2007-11-20
  • 打赏
  • 举报
回复
不过效果不好,没有达到预期目的,根本没有拦截!~
fys249931556 2007-11-20
  • 打赏
  • 举报
回复
谢谢赐教.. 领教啦.@!~!~
nicholsky 2007-11-19
  • 打赏
  • 举报
回复
/xxxxxxx/.test(xxxxxxxx)这种写法学习了。
mingxuan3000 2007-11-19
  • 打赏
  • 举报
回复
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>replaceHref</title>
</head>

<body>


<script >
function cc(){
if(/(^ *| *$)/.test(document.getElementsByName("aaa")[0].value)){
alert(" :have")
return false;
}
}
</script>
<form name= "form1 " method= "post " action= "http://www.csdn.net " onsubmit="return cc()" >
<input name= "aaa" type= "text " />
<input type=submit name=button value= "bbb " />
</form>
</body>

</html>



另 name不要用id这种关键字

87,910

社区成员

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

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