javascript表单验证

一、必填(或必选)项目

<script type="text/javascript">// <![CDATA[
function validate_required(field,alerttxt)
     {
       with(field)
       if(value==null||value=="")
        {
           alert(alerttxt);return false
        }
         else
        {
           return true
        }
     }

     function validate_form(thisfrom)
    {
       with(thisform)
      if(validate_required(email,"Email must be filled out!")==false)
      {email.focus;return false}
    }
</script>

Email验证,输入的数据必须包含 @ 符号和点号(.)。同时,@ 不可以是邮件地址的首字符,并且 @ 之后需有至少一个点号:

<html>
<head>
<script type="text/javascript">
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<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 address!")==false)
  {email.focus();return false}
}
}
</script>
</head>

<body>
<form action="submitpage.htm"onsubmit="return validate_form(this);" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit"> 
</form>
</body>

</html>

  

 

你可能感兴趣的:(javascript表单验证)