Home >Web Front-end >JS Tutorial >JS method to verify IP, subnet mask, gateway and MAC_javascript skills
The example in this article describes the JS method of verifying IP, subnet mask, gateway and MAC. Share it with everyone for your 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>测试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不能为空"); myform.focus(); return false; } //验证ip合法性 if(myform.ip.value.match(reg)) { // return true; } else { alert ("IP不合法"); myform.focus(); return false; } if(myform.zwym.value == "") { alert ("子网掩码不能为空"); myform.focus(); return false; } //验证子网掩码合法性 if(myform.zwym.value.match(reg)) { // return true; } else { alert ("子网掩码不合法"); myform.focus(); return false; } // if(myform.gateway.value == "") { alert ("网关不能为空"); myform.focus(); return false; } //验证网关合法性 if(myform.gateway.value.match(reg)) { // return true; } else { alert ("网关不合法"); myform.focus(); return false; } //MAC if(myform.mac.value == "") { alert ("MAC不能为空"); myform.focus(); return false; } //验证mac合法性 if(myform.mac.value.match(reg)) { // return true; } else { alert ("MAC不合法"); 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 地址:</div></td> <td width="506" height="53"> <input type="text" name="ip" size="15" maxlength="15" /> <font color="#FF0000">*</font>不能为空,必须填写</td> </tr> <tr> <td width="186" height="45" ><div align="center">子网掩码:</div></td> <td width="506" height="45" > <input type="text" name="zwym" size="15" maxlength="15" /> <font color="#FF0000">*</font>不能为空,必须填写</td> </tr> <tr> <td width="186" height="46" ><div align="center">网 关:</div></td> <td width="506" height="46"> <input type="text" name="gateway" size="15" maxlength="15" /> <font color="#FF0000">*</font>不能为空,必须填写</td> </tr> <tr> <td width="186" height="48"><div align="center">MAC地址:</div></td> <td width="506" height="48" > <input type="text" name="mac" size="15" maxlength="15" /> <font color="#FF0000">*</font>不能为空,必须填写</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="提交" /> <input type="reset" value="重置" /> </td> </tr> </table> </form> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.