javascript 如何验证

来源:百度知道 编辑:UC知道 时间:2024/06/06 16:42:03
<script type="text/javascript">
function check(){
if(document.forms[0].elements[0].value.length == 0){
window.alert("不能为空!");
return false;
}
if(document.forms[0].elements[1].value.length == 0){
window.alert("不能为空!");
return false;
}
if(document.forms[0].elements[2].value.length == 0){
window.alert("不能为空!");
return false;
}
return true;
}
</script>

<body>
<CENTER>
<html:form action="/stu.do?stuAction=insertStu" onsubmit="return check()">
学生名 :<html:text property="stu.stuName"></html:text><br>
学生年龄 :<html:text property="stu.stuAge"></html:text><br>
学生性别 :<html:radio property="stu.stuSex" value="男">男</html:radio>
<html:radio property=&qu

给你一个例子,照着做就OK了。

<script language="javascript">
function on_submit(){
if(form.name.value!=12)
{
alert("请输入12个字符!");
form.name.focus();
return false;
}
}
</script>

<html>
<head>

<script LANGUAGE=javascript>
<!--
function check()
{

if (document.Form.username.value=="" )
{
alert("请填写您的用户名!");
document.Form.username.focus();
return false;
}

var filter=/^\s*[.A-Za-z0-9_-]{5,15}\s*$/;
if (!filter.test(document.Form.username.value)) {
alert("用户名填写不正确,请重新填写!可使用的字符为(A-Z a- z 0-9 _ - .)长度不小于5个字符,不超过15个字符,注意不要使用空格。");
document.Form.username.focus();

return (false);
}

if (document.Form.password.value=="")
{
alert("请填写您的密码!");
document.Form.password.focus();