js表单验证

g375636389 2009-09-07 10:23:12
<input id="id1" type="text" /><br />
<input id="id2" type="password" /><br />
<input id="id3" type="password" /><br />
<input id="Button1" type="button" value="提交" onclick="but_click();" />
当点击提交按钮的时候多id1,id2,id3 进行验证.1,输入不能为空;2必须正确输入;
...全文
54 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hookee 2009-09-07
  • 打赏
  • 举报
回复

<script>
function b1(){
var t1 = document.getElementById("id1").value;
t1 = t1.replace(/(^\s+|\s+$)/g,"");
if(t1==""){
alert("不能为空");
return false;
}
else return true;
}
function b2(){
var t2 = document.getElementById("id2").value;
t2 = t2.replace(/(^\s+|\s+$)/g,"");
if(t2==""){
alert("密码1不能为空");
return false;
}
return true;
}
function b3(){
var t3 = document.getElementById("id3").value;
t3 = t3.replace(/(^\s+|\s+$)/g,"");
if(t3==""){
alert("密码2不能为空");
return false;
}
return true;
}

function but_click(){
var f = document.forms[0];
var t2 = document.getElementById("id2").value;
var t3 = document.getElementById("id3").value;
b = b1() & b2() & b3();
if(!b) return false;
if(t2!=t3){
alert("密码不相等");
return false;
}
f.submit()
}
</script>
<form action="xxx" method="post">
<input id="id1" type="text" onblur="b1()"/> <br />
<input id="id2" type="password" onblur="b2()"/> <br />
<input id="id3" type="password" onblur="b3()"/> <br />
<input id="Button1" type="button" value="提交" onclick="but_click();" />
</form>

Bird_fro 2009-09-07
  • 打赏
  • 举报
回复
<input id="id1" type="text" onblur="but_click()" /> <br />
<input id="id2" type="password" onblur="but_click()" /> <br />
<input id="id3" type="password" onblur="but_click()" /> <br />
g375636389 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hookee 的回复:]
HTML code<script>function but_click(){var f= document.forms[0];var t1= document.getElementById("id1").value;var t2= document.getElementById("id2").value;var t3= document.getElementById("id3").value; ¡­
[/Quote]
如果我id1,id2,id3 都已经有onblur 事件验证了...怎么直接调用
BeenZ 2009-09-07
  • 打赏
  • 举报
回复

var id1=document.getElementById("id1");
var id2=document.getElementById("id2");
var id3=document.getElementById("id3");
if(id1.innerText&&id2.innerText&&id3.innerText){
alert("null")
return false;
}
if(id1.innerText!="xxxxx"&&id2.innerText!="xxxx"&&id3.innerText!="xxx") return false
return true
hookee 2009-09-07
  • 打赏
  • 举报
回复

<script>
function but_click(){
var f = document.forms[0];
var t1 = document.getElementById("id1").value;
var t2 = document.getElementById("id2").value;
var t3 = document.getElementById("id3").value;
t1 = t1.replace(/(^\s+|\s+$)/g,"");
t2 = t2.replace(/(^\s+|\s+$)/g,"");
t3 = t3.replace(/(^\s+|\s+$)/g,"");
if(t1=="" || t2=="" || t3==""){
alert("不能为空");
return false;
}
if(t2!=t3){
alert("密码不相等");
return false;
}
f.submit()
}
</script>
<form action="xxx" method="post">
<input id="id1" type="text" /> <br />
<input id="id2" type="password" /> <br />
<input id="id3" type="password" /> <br />
<input id="Button1" type="button" value="提交" onclick="but_click();" />
</form>

87,903

社区成员

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

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