正则表达式的问题,我想一天了。请高人指点!

mmqqr 2010-02-06 09:43:40
我想用正则表达示限制所输入的值不为空并且不含有空格,正则表达式是:"^\\S+$",现在的问题是,我在function{}中指定patt的值为"^\\S+$",此时能达到限制目的。但是如果我用var patt = document.form1.che.check 语句的话,如果所输入字符中没有空间也提示错误,这个问题我想一天了,请高人指点谢谢。
<script type="text/javascript">
function CheckForm(oForm)
{
//取得表单的值,用通用取值函数
// var patt = document.form1.che.check;
var sVal = document.form1.che.value;
patt = "^\\S+$";
var reg = new RegExp(patt);
if (!reg.test(sVal))
{
//验证不通过,弹出提示warning
alert(document.form1.che.warning);
return false;
}
return true;
}
</script>
</head>
<body>
<form onsubmit="return CheckForm(this)" name="form1">
<input type="text" name="che" check="^\\S+$" warning="id不能为空,且不能含有空格">
<input type=submit />
</form>
</body>


...全文
130 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmqqr 2010-02-07
  • 打赏
  • 举报
回复
楼上的几种方法,我都试过了,还是不能解决,麻烦大家了,还有解决的办法吗????我用二楼的方法的效果和我以前的效果一样。
renzaijiang 2010-02-07
  • 打赏
  • 举报
回复
<script type="text/javascript">
function CheckForm(oForm)
{
if (/\s+|^$/.test(document.getElementById("che").value))
{
alert("输入不能为空或者带空格!");
return false;
}
return true;
}
</script>
</head>

<form onsubmit="return CheckForm(this)" name="form1">
<input type="text" id="che" />
<input type=submit />
</form>


这样也行的 更简洁 希望
renzaijiang 2010-02-07
  • 打赏
  • 举报
回复
<script type="text/javascript">
function CheckForm(oForm)
{
if (/\s+/.test(document.getElementById("che").value)||document.getElementById("che").value=="")
{
alert("输入不能为空或者带空格!");
return false;
}
return true;
}
</script>
</head>

<form onsubmit="return CheckForm(this)" name="form1">
<input type="text" id="che" />
<input type=submit />
</form>


弄本标准点的书吧 你弄的这些只支持ie
wcwtitxu 2010-02-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zswang 的回复:]
<input type="text" id="che" name="che" check="^\S+$" warning="id不能为空,且不能含有空格">[/Quote]

王集鹄 2010-02-07
  • 打赏
  • 举报
回复

<input type="text" id="che" name="che" check="^\S+$" warning="id不能为空,且不能含有空格">

html里的属性值不需要转义
你自己在这句之前打印一下看看就知道区别了
...
alert(patt); /// <<<<
var reg = new RegExp(patt);
...

/^\\S+$/
/^\S+$/
xuld 2010-02-06
  • 打赏
  • 举报
回复
daxian520 2010-02-06
  • 打赏
  • 举报
回复
正确的写法
是getElementsByTagName而不是getElementByTagName
document.getElementsByTagName("input").lastChild.attribute["check"].value
daxian520 2010-02-06
  • 打赏
  • 举报
回复
或者
document.getElementByTagName("input")lastChild.attribute["check"].value
xuld 2010-02-06
  • 打赏
  • 举报
回复
document.form1.che.check不是标准

使用:


document.getElementByTagName("input")[0].attribute["check"].value
hookee 2010-02-06
  • 打赏
  • 举报
回复
用getAttribute取自定义属性

<script type="text/javascript">
function CheckForm(oForm)
{
//取得表单的值,用通用取值函数
var patt = document.getElementById("che").getAttribute("check");
var sVal = document.form1.che.value;
// patt = "^\\S+$";
var reg = new RegExp(patt);
if (!reg.test(sVal))
{
//验证不通过,弹出提示warning
alert(document.getElementById("che").getAttribute("warning"));
return false;
}
return true;
}
</script>
<form onsubmit="return CheckForm(this)" name="form1">
<input type="text" id="che" name="che" check="^\\S+$" warning="id不能为空,且不能含有空格">
<input type="submit" />
</form>

87,997

社区成员

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

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