不允许的特殊字符提示

时间:2019-03-15 02:24:52

标签: python list function

因此,我正在编写一个检查电子邮件字符中的错误的项目。我在这里搜索的内容没有任何帮助,在duckduckgo上也没有。一切正常,但是我在shell上看到以下内容:

e mail: abc @ xyz.com   *** ERROR: 2. The number of @'s in your email is suspect. ***
e mail: .abc@xyz.com    *** ERROR: 2. The number of @'s in your email is suspect. ***
e mail: abc@xyz.c   *** ERROR: 2. The number of @'s in your email is suspect. ***

注意testAtsign函数(2. The number of @'s in your email is suspect.)是一种占用空间的地方,应该显示其他错误,例如不允许的特殊字符?

我认为我的testSpecialChars函数存在问题,该问题允许testAtsign函数接管。不允许的列表可能有问题吗?

任何想法都会受到赞赏。

emailList = ["abc@xyz.com",
             "abc@@xyz.com",
             "@xyz.com",
             "abc.xyz.com",
             "abc@x.yz",
             "abc@xyz.c",
             "a@b.c",
             "abc@xyz..com",
             "abc.@xyz.com",
             "abc@.xyz.com",
             "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@aaaaaa.aaaaa",
             "' or 1=1 '==",
             "abc@xyz.$%",
             "abc@xyz.()",
             "abc'@xyz.com",
             "aaaaa@aaaaa",
             "abc @ xyz.com",
             ".abc@xyz.com",
             "abc@xyz.c"]



def errorMessage(email, error):
    print("e mail: {} \t*** ERROR: {} ***".format(email, error))

def testAtsign (email):
    if "@" in email:
        #should be 1 @
        #are there more?
        atCount = 0
        for character in email:
            if character == "@":
                atCount += 1

            if atCount != 1:
                errorMessage(email, "2. The number of @'s in your email is suspect.")
                return True
            elif email[0] == "0":
                errorMessage(email, "3.The @ is not in a valid position.")
                return True
            else:
                testLast5 = email[-5]
                if "@" in testLast5:
                    errorMEssage(email, "4. Okay, your @ is not in the last 5 characters, whats up with you?")
                    return True
                else:
                    return False
    else:
        errorMessage(email, "5. your @ is missing")
        return True


def testDot(email):
    if "." in email:
        #has to be at least ONE

        if email[0] == ".":
            errorMessage(email, "10. Your '.' is in the first position.")
            return True
        testLast2 = email[-2:]
        if "." in testLast2:
            errorMessage(email, "11. Your '.' is in the last position.")
            return True
        #should not be doubled or next to @
        elif ".." in email or ".@" in email or "..@" in email or "@." in email or "@.." in email:
            errorMessage(email, "6. Were sensing an erorr in your '.' config.")
            return True

    else:
        errorMessage(email, "7. Where is the '.'?")
        return True

def testSpecialChars(email) :
    #first test for spaces
    if " " in email:
        errorMessage(email, "8. We dont allow spaces in our emails here.")
        return True

    #create list of unallowables
    unallowable = "! # $ % ^ & * ( ) : ; < > ? / { } =".split()
    #add quotes
    unallowable.append('"')
    unallowable.append("'")

    for character in email:
        if character in unallowable:
            errorMEssage(email, "9. Character {} is not allowed".format(character))
            return True









for email in emailList:
    foundError = False
    if len(email) < 7 or len(email) > 30:
        errorMessage(email, "1. Invalid Length")  #labeling the errors with numbers to keep track
        foundError = True

    if not foundError:
        foundError = testAtsign(email)

    if not foundError:
        foundError = testDot(email)
    if not foundError:
        foundError = testSpecialChars(email)

    if not foundError:
        print("Rad, your email seems valid.".format(email))

    print("flag")

2 个答案:

答案 0 :(得分:0)

您首先要检查testAtsign()。在函数内部,如果检测到任何错误,则将FoundError的值更改为“ True”。由于foundError的值已更新为True,因此所有其他“ if”条件(对于testDot()和testSpecialChars())都会失败并且根本不会执行。与if语句if len(email) < 7 or len(email) > 30相同。当这是真的,并且foundError在其中被更新为True的那一刻,即使testAtSign()也不会执行。

答案 1 :(得分:0)

在此功能中,testAtsign代码在电子邮件地址上循环以计数“ @”字符,并在循环内对计数进行计数。。因此,除非地址中的第一个字符为“ @”,否则该函数将始终返回True,因为“ @”的计数不等于1。

通过将检查移到循环外来解决此问题;实际上,可以使用Python字符串的count方法来获取每个地址中“ @”字符的数量,从而完全删除该循环。

固定功能如下:

def testAtsign(email):
    if "@" in email:
        # should be 1 @
        # are there more?
        atCount = email.count("@")

        if atCount != 1:
            errorMessage(email, "2. The number of @'s in your email is suspect.")
            return True
        elif email[0] == "0":  # <- this might need fixing?
            errorMessage(email, "3.The @ is not in a valid position.")
            return True
        else:
            testLast5 = email[-5]  # <- this only gets a single character
            if "@" in testLast5:
                errorMessage(
                    email,
                    "4. Okay, your @ is not in the last 5 characters, whats up with you?",
                )
                return True
            else:
                return False
    else:
        errorMessage(email, "5. your @ is missing")
相关问题