使用一个条件语句实现,这个表单如下: <form name="register" method="POST" action onSubmit="return check();"> <p align="center"> Email:<input type="text" name="email" size="20"> <input type="submit" value="提交" name="B1"> <input type="reset" value="重填" name="B2"> </p> </form>
当客户端提交时触发OnSubmit事件调用检查函数进行判断,这个函数如下:
function check() { if(document.register.email.value.length!=0) { if (document.register.email.value.charAt(0)=="." document.register.email.value.charAt(0)=="@" document.register.email.value.indexOf('@', 0) == -1 document.register.email.value.indexOf('.', 0) == -1 document.register.email.value.lastIndexOf("@") ==document.register.email.value.length-1 document.register.email.value.lastIndexOf(".") ==document.register.email.value.length-1) { alert("Email的格式不正確!"); document.register.email.focus(); return false; } } else { alert("Email的不能为空!"); document.register.email.focus(); return false; } return false; }
|