x=document.myForm at=x.email.value.indexOf("@") if (at == -1) { alert("Not a valid e-mail") return false } } </script> </head>
<body> <form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate()"> Enter your E-mail: <input type="text" name="email" size="20"> <input type="submit" value="Submit"> </form> <p><b>Note:</b> This example ONLY tests if the e-mail address contains an "@" character. A "real-life" code would have to test for punctuations, spaces and other things as well.</p> </body>
<head> <script type="text/javascript"> function validate() { x=document.myForm txt=x.myInput.value if (txt>=1 && txt<=5) { return true } else { alert("Must be between 1 and 5") return false } } </script> </head>
<body> <form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate()"> Enter a value from 1 to 5: <input type="text" name="myInput" size="20"> <input type="submit" value="Submit"> </form> </body>
</html>
输入特定长的字符才能提交表单
<html>
<head> <script type="text/javascript"> function validate() { x=document.myForm input=x.myInput.value if (input.length>5) { alert("The field cannot contain more than 5 characters!") return false } else { return true } } </script> </head>
<body> <form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate()"> Enter some text (you will get a message if you add more than 5 characters): <input type="text" name="myInput" size="20"> <input type="submit" value="Submit"> </form> </body>
if (at==-1) { alert("Not a valid e-mail!") submitOK="False" } if (code<1 code>5) { alert("The value must be between 1 and 5") submitOK="False" } if (firstname.length>10) { alert("Your name must be less than 10 characters") submitOK="False" } if (submitOK=="False") { return false } } </script> </head>