正则表达式的问题,我想一天了。请高人指点! 本帖最后由 mmqqr 于 2010-02-06 21:45:51 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用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> document.form1.che.check不是标准使用:document.getElementByTagName("input")[0].attribute["check"].value 或者document.getElementByTagName("input")lastChild.attribute["check"].value 正确的写法是getElementsByTagName而不是getElementByTagNamedocument.getElementsByTagName("input").lastChild.attribute["check"].value <input type="text" id="che" name="che" check="^\S+$" warning="id不能为空,且不能含有空格"> html里的属性值不需要转义你自己在这句之前打印一下看看就知道区别了...alert(patt); /// <<<<var reg = new RegExp(patt); .../^\\S+$//^\S+$/ <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 <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> 这样也行的 更简洁 希望 修改下面的JS代码使地区子类直接显示在地区父类下面 cookie属性在登陆界面的使用问题 JS新手求教! js动态生成表格中的动画效果实现! 求自动发帖程序的思路 firfox下表单验证的问题 Javascript 获取css属性问题 求一个2级菜单的做法 关于javascript的开发工具! 这个效果是怎么做出来的 Extjs comboboxtree提交表单时获取不到值 新学JS 请问“跨域”指的是什么? 《JavaScript高级程序设计(第2版)》样章抢先发布
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>
document.getElementByTagName("input")[0].attribute["check"].value
document.getElementByTagName("input")lastChild.attribute["check"].value
是getElementsByTagName而不是getElementByTagName
document.getElementsByTagName("input").lastChild.attribute["check"].value
<input type="text" id="che" name="che" check="^\S+$" warning="id不能为空,且不能含有空格"> html里的属性值不需要转义
你自己在这句之前打印一下看看就知道区别了
...
alert(patt); /// <<<<
var reg = new RegExp(patt);
.../^\\S+$/
/^\S+$/
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
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>
这样也行的 更简洁 希望