JavaScript 学习笔记(六)with(field) {……value.indexOf("@");……}、.focus()

来源:互联网 发布:4g网络架构 编辑:程序博客网 时间:2024/06/05 06:40

js表单验证

function validate_required(field,alerttxt){with(field) {if(value==null || value=="") {alert(alerttxt);return false;}else {return true;}}}function validate_form(thisform) {with(thisform) {if(validate_required(uname,"user name must be filled out!")==false) {uname.focus();return false;}}}
<form id="form" action="submitpage.html" method="post" onsubmit="return validate_form(this)"><div>用户名:<input type="text" name="uname" /><input type="submit" value="提交" /></div></form>

END

function validate_email(field,alerttxt) {with(field) {apos=value.indexOf("@");//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置dotpos=value.lastIndexOf(".");//lastIndexOf方法返回子字符串最后一次出现的位置,和 indexof相反if(dotpos<1 || dotpos-apos<2) {alert(alerttxt);return false;}else {return true;}}}function validate_form(thisform) {with(thisform) {if(validate_email(email,"Not a valid e-mail adress!")==false) {email.focus();return false;}}}
<form id="form1" action="submitpage.html" method="post" onsubmit="return validate_form(this)"><div>E-mail:<input type="text" name="email" /><input type="submit" value="提交" /></div></form>

END

原创粉丝点击