JS validates IP subnet mask gateway and MAC methods

  • 2020-06-22 23:53:16
  • OfStack

This article illustrates JS's methods for verifying IP, subnet mask, gateway, and MAC. Share to everybody for everybody reference. The details are as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> test IP</title>
</head><script language="javascript">
<!---
  function checkdata()
  {
  var reg = /^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])(/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])){3}$/;
  if(myform.ip.value == "")
  {
   alert ("IP Can't be empty ");
   myform.focus();
   return false;
  }
   // validation ip legitimacy 
   if(myform.ip.value.match(reg)) 
   {  
   // return true;
   }
   else 
   {
   alert ("IP illegal ");
   myform.focus();
   return false;
   }
  if(myform.zwym.value == "")
  {
   alert (" The subnet mask cannot be empty ");
   myform.focus();
   return false;
  }
  // Verify the validity of the subnet mask 
   if(myform.zwym.value.match(reg)) 
   {  
   // return true;
   }
   else 
   {
   alert (" The subnet mask is not valid ");
   myform.focus();
   return false;
   }
   //
  if(myform.gateway.value == "")
  {
   alert (" The gateway cannot be empty ");
   myform.focus();
   return false;
  }
   // Verify gateway validity 
   if(myform.gateway.value.match(reg)) 
   {  
   // return true;
   }
   else 
   {
   alert (" Illegal gateway ");
   myform.focus();
   return false;
   }
   //MAC
   if(myform.mac.value == "")
  {
   alert ("MAC Can't be empty ");
   myform.focus();
   return false;
  }
   // validation mac legitimacy 
   if(myform.mac.value.match(reg)) 
   {  
   // return true;
   }
   else 
   {
   alert ("MAC illegal ");
   myform.focus();
   return false;
   }
 }   
//--> 
</script><body>
<form method="post" name="myform" onSubmit="return checkdata();">
    <table width="708" height="253" border="1" id="table1"  >
    <tr>
     <td width="186" height="53" ><div align="center">IP  address :</div></td>
     <td width="506" height="53">&nbsp;&nbsp;
     <input type="text" name="ip" size="15" maxlength="15" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000">*</font> Cannot be empty, must be filled in </td>
    </tr>
    <tr>
     <td width="186" height="45" ><div align="center"> Subnet mask :</div></td>
     <td width="506" height="45" >&nbsp;&nbsp;&nbsp;<input type="text" name="zwym" size="15" maxlength="15" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000">*</font> Cannot be empty, must be filled in </td>
    
    </tr>
    <tr>
     <td width="186" height="46" ><div align="center"> net   guan :</div></td>
     <td width="506" height="46">&nbsp;&nbsp;
     <input type="text" name="gateway" size="15" maxlength="15" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000">*</font> Cannot be empty, must be filled in </td>
    </tr>
    <tr>
     <td width="186" height="48"><div align="center">MAC address :</div></td>
     <td width="506" height="48" >&nbsp;&nbsp;
     <input type="text" name="mac" size="15" maxlength="15" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000">*</font> Cannot be empty, must be filled in </td>
    </tr>
    <tr><!---changeinfo(ip.value,zwym.value,mac.value,reboot)-->
    <!-- onClick="changeinfo(ip.value,zwym.value,gateway.value,mac.value)"---->
     <td height="47" colspan="2" align="center"><input type="submit" value=" submit " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value=" reset " />
     </td>
    </tr>
    </table>
   </form>
</body>
</html>

Hopefully, this article has been helpful in your javascript programming.


Related articles: