网络编程
位置:首页>> 网络编程>> Python编程>> PyQt5 QLineEdit输入的子网字符串校验QRegExp实现

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

作者:皓月盈江  发布时间:2022-05-08 16:47:58 

标签:PyQt5,QLineEdit,校验

自己编写的用于对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

来源:https://blog.csdn.net/u013541325/article/details/115430855

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com