PyQt5 QLineEdit输入的子网字符串校验QRegExp实现

自己编写的用于对lineEdit编辑框输入的子网,例如:192.168.60.1/24字符串校验是否合规。

PyQt5 QLineEdit输入的子网字符串校验QRegExp实现

# 限制lineEdit编辑框只能输入./字符和数字
reg = QRegExp(\'[0-9./]+$\')
validator = QRegExpValidator(self)
validator.setRegExp(reg)
self.lineEditSubNet.setValidator(validator)
 def SubnetVerification(self, strTempSubNet):
  \"\"\"
  对输入的子网字符串进行校验
  \"\"\"
  # 对输入的交换机子网地址及子网掩码格式进行校验
  if strTempSubNet.count(\'/\') == 1:
   pass
  else:
   # 警告信息框
   win32api.MessageBox(0, \"请输入正确的子网,例:192.168.60.1/24\", \"温馨提示\", win32con.MB_ICONWARNING)
   return False

  strListNet = strTempSubNet.split(\'/\')

  if strListNet[0] != \'\' and strListNet[1] != \'\':
   pass
  else:
   # 警告信息框
   win32api.MessageBox(0, \"请输入正确的子网,例:192.168.60.1/24\", \"温馨提示\", win32con.MB_ICONWARNING)
   return False

  self.strIP = strListNet[0]
  self.strSubMaskNum = strListNet[1]
  # print(self.strIP)
  # print(self.strSubMaskNum)

  if 1 <= int(self.strSubMaskNum, 10) <= 32:
   pass
  else:
   # 警告信息框
   win32api.MessageBox(0, \"请输入正确的子网,例:192.168.60.1/24\", \"温馨提示\", win32con.MB_ICONWARNING)
   return False

  # 对输入的交换机子网地址进行校验
  # 判断是否符合IP地址中有3个.
  if self.strIP.count(\'.\') == 3:
   pass
  else:
   # 警告信息框
   win32api.MessageBox(0, \"请输入正确的子网,例:192.168.60.1/24\", \"温馨提示\", win32con.MB_ICONWARNING)
   return False

  strList = self.strIP.split(\".\")
  # print(strList)
  if strList[0] != \'\' and strList[1] != \'\' and strList[2] != \'\' and strList[3] != \'\':
   pass
  else:
   # 警告信息框
   win32api.MessageBox(0, \"请输入正确的子网,例:192.168.60.1/24\", \"温馨提示\", win32con.MB_ICONWARNING)
   return False

  nList = list(map(int, strList))

  if 0 <= nList[0] <= 255 and 0 <= nList[1] <= 255 and 0 <= nList[2] <= 255 and 0 <= nList[3] <= 255:
   pass
  else:
   # 警告信息框
   win32api.MessageBox(0, \"请输入正确的子网,例:192.168.60.1/24\", \"温馨提示\", win32con.MB_ICONWARNING)
   return False

  return True

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容