Java background developed form validation before submission

  • 2020-06-03 06:24:40
  • OfStack

Without further ado, the specific code is as follows:


<%@ page language="java" contentType="text/html; charset=UTF-8" 
  pageEncoding="UTF-8"%> 
<%@ page import="java.util.*" %> 
<%@ page import="java.sql.*" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title> The form submission </title> 
  </head> 
  <body> 
    <p align="center"><b> The form submission </b><br></p> 
    <center> 
      <div align="left"> 
        <table height="60" border="0" align="left"> 
          <tr> 
            <td> 
              <form name="channelform" action="addChannel.jsp" 
               onsubmit="return validate_channel_info(this);" method="post"> 
                 The name :  
                <input type="text" name="channelname" /> 
                <br /> 
                ID:  
                <input type="text" name="channelid" /> 
                <br /> 
                <input type="submit" value=" submit ">                  
              </form> 
            </td> 
          </tr> 
        </table>        
         
        <script type="text/javascript"> 
          function validate_channel_info(channelform) 
          { 
            if(channelform.channelname.value=="") 
            { 
              alert(" Please enter the correct name "); 
              return false; 
            } 
            else if(!isNumber(channelform.channelid.value)) 
            { 
              alert(" Please enter legal ID"); 
              return false; 
            } 
            return true; 
          }  
          function isNumber(str)     //  Determine if it is a non-negative integer  
          { 
            var rx = /^[0-9]+$/; 
            return rx.test(str); 
          } 
        </script> 
      </div> 
    </center> 
  </body> 
</html> 

Related articles: