美文网首页
11 js之表单验证

11 js之表单验证

作者: 一枝妖孽 | 来源:发表于2018-06-01 10:56 被阅读0次

    JavaScript 表单验证

    1 必填(或必选)项目

    <html>
    <head>
    <script type="text/javascript">
    
    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(email,"Email must be filled out!")==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>
    

    2 E-mail 验证

    <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>
    

    相关文章

      网友评论

          本文标题:11 js之表单验证

          本文链接:https://www.haomeiwen.com/subject/alphsftx.html