0
4

More than 1 year has passed since last update.

Pythonによる各種入力チェック関数の実装

Posted at

概要

Pythonによる各種入力チェック実装。
サーバーサイドでのチェックを想定しています。
実装するチェックは以下になります。
・必須チェック
・上限桁数判定
・型判定
・半角英数判定
・Base64判定

ソースコード

checkUtil.py
import re

def checkRequire(checkStr: str):
  # 必須チェック判定
  return len(checkStr)!=0

def checkMaxNum(checkStr: str, maxNum: int):
  # 上限桁数判定
  currentNum = len(checkStr)
  return currentNum <= maxNum

def checkType(checkStr: str, type):
  # 型判定
  if checkStr == None:
    return True
  return type(checkStr) == type

def checkAlphaNum(checkStr: str):
  # 半角英数判定
  alphaNum = re.compile((r'^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$')
  return alphaNum.match(checkStr) is not None

def checkBase64(checkStr: str):
  # Base64判定
  Base64 = re.compile(r'^[a-zA-Z0-9=+/:;,]+$')
  return Base64.match(checkStr) is not None


0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4