#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Email validation check help


    Hi everyone,

    I am having a few issues with an email validation programme I am currently trying to write.

    My actual task is below:
    When entering email addresses systems often use a simple form of validation to ensure that the
    address entered is possible.
    For the purposes of this task an email address must:
    Start with a string of alphanumeric characters
    followed by the @ symbol
    another string of alphanumeric characters
    followed by a .
    then a string of alphanumeric characters.
    Eg a@b.c and ab23@f45.d3 are both valid
    but @bc.d and 123.c@cvb are not valid
    Design code and test a program to:
    Accept an input string and check its validity.
    Output VALID or INVALID accordingly.


    I have managed to get the code to read it back to me, with capitalised letters and lowercase, whichever I type in:

    PHP Code:
    # python shell to check email address
    CountDot 0
    CountAmpasand 
    0
    ErrorFlag 
    False
    print ("Please enter your email address:")
    email input()
    print (
    email)
    Loops len(email)
    email email.lower()
    print (
    email
    I need some guidance to get as close to the original task, but keeping the actual python code simple for a user to read where they can read which each step is doing.

    I think I have covered everything in the post which I am asking help for, many thanks in advance if you can help me out with this.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Expectation: minimally, you will learn the term "finite state machine", about assertions which can test code, that you should test your code.
    Code:
    '''
        # The easy way (which by the way probably works similarly in spirit to this program)
    
        import re
    
        print(not not re.match('[a-zA-Z0-9]+@[a-zA-Z0-9]+[.][a-zA-Z0-9]+', candidate_email_address))
    
        function fsm implements a somewhat general purpose finite state machine.
        State transition tables and actions, as well as the data maps are external to fsm.
        Additionally, the source provides tables that solve the problem of this thread.
    '''
    
    
    import string
    
    def classify(c,*args):
        '''
            return a numeric classification of the input character c
        '''
        i = 0
        for s in args:
            if c in s:
                break
            i += 1
        return i
    
    ALPHANUMERIC = set(string.digits+string.ascii_letters)
    AT = '@'
    DOT = '.'
    
    assert 0 == classify('Z', ALPHANUMERIC, AT, DOT)  # testing
    assert 1 == classify('@', ALPHANUMERIC, AT, DOT)  # testing
    assert 2 == classify('.', ALPHANUMERIC, AT, DOT)  # testing
    assert 3 == classify('!', ALPHANUMERIC, AT, DOT)  # testing
    
    
    
    # the interior tuples represent (the next state, action)
    # actions are 0 indicating error, and 1 indicating success.
    states_and_actions = [
        #alnum at    dot   invalid    
        [(0,1),(1,1),(3,0),(3,0)], # state 0.  Initial state and gather name part of email. @ or bad character advance to new state
        [(1,1),(3,0),(2,1),(3,0)], # state 1.  Place.  "." or bad character transition to a new state
        [(2,1),(3,0),(3,0),(3,0)], # state 2.  Affiliation.  invalid character changes state.
        [(3,0),(3,0),(3,0),(3,0)], # state 3.  Error.
        ]
    
    
    def fsm(state_and_action, character_map, data):
        state = action = 0
        for c in data:
            column_index = classify(c, *character_map)
            (state, action,) = state_and_action[state][column_index]
        return action
    
    MAP = (ALPHANUMERIC, AT, DOT,)
    assert     fsm(states_and_actions, MAP, 'me@my.org')
    assert     fsm(states_and_actions, MAP, 'valid993@21email.ad823dress')
    assert not fsm(states_and_actions, MAP, 'invalid email address')
    assert not fsm(states_and_actions, MAP, 'invalid@email address')
    assert not fsm(states_and_actions, MAP, 'invalid@email.address.com')
    
    
    
    #fsm(states_and_actions, MAP, candidate_email_address) # implements "my actual task"
    Last edited by b49P23TIvg; January 31st, 2014 at 10:15 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Expectation: minimally, you will learn the term "finite state machine", about assertions which can test code, that you should test your code.
    Code:
    '''
        # The easy way (which by the way probably works similarly in spirit to this program)
    
        import re
    
        print(not not re.match('[a-zA-Z0-9]+@[a-zA-Z0-9]+[.][a-zA-Z0-9]+', candidate_email_address))
    
        function fsm implements a somewhat general purpose finite state machine.
        State transition tables and actions, as well as the data maps are external to fsm.
        Additionally, the source provides tables that solve the problem of this thread.
    '''
    
    
    import string
    
    def classify(c,*args):
        '''
            return a numeric classification of the input character c
        '''
        i = 0
        for s in args:
            if c in s:
                break
            i += 1
        return i
    
    ALPHANUMERIC = set(string.digits+string.ascii_letters)
    AT = '@'
    DOT = '.'
    
    assert 0 == classify('Z', ALPHANUMERIC, AT, DOT)  # testing
    assert 1 == classify('@', ALPHANUMERIC, AT, DOT)  # testing
    assert 2 == classify('.', ALPHANUMERIC, AT, DOT)  # testing
    assert 3 == classify('!', ALPHANUMERIC, AT, DOT)  # testing
    
    
    
    # the interior tuples represent (the next state, action)
    # actions are 0 indicating error, and 1 indicating success.
    states_and_actions = [
        #alnum at    dot   invalid    
        [(0,1),(1,1),(3,0),(3,0)], # state 0.  Initial state and gather name part of email. @ or bad character advance to new state
        [(1,1),(3,0),(2,1),(3,0)], # state 1.  Place.  "." or bad character transition to a new state
        [(2,1),(3,0),(3,0),(3,0)], # state 2.  Affiliation.  invalid character changes state.
        [(3,0),(3,0),(3,0),(3,0)], # state 3.  Error.
        ]
    
    
    def fsm(state_and_action, character_map, data):
        state = action = 0
        for c in data:
            column_index = classify(c, *character_map)
            (state, action,) = state_and_action[state][column_index]
        return action
    
    MAP = (ALPHANUMERIC, AT, DOT,)
    assert     fsm(states_and_actions, MAP, 'me@my.org')
    assert     fsm(states_and_actions, MAP, 'valid993@21email.ad823dress')
    assert not fsm(states_and_actions, MAP, 'invalid email address')
    assert not fsm(states_and_actions, MAP, 'invalid@email address')
    assert not fsm(states_and_actions, MAP, 'invalid@email.address.com')
    
    
    
    #fsm(states_and_actions, MAP, candidate_email_address) # implements "my actual task"
    Thanks for your reply.

    When I run this programme, it comes up with a syntax error with the '@' highlighted indicating a problem with it.

    I have experimented with it and cannot find the problem.

    Thanks
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Program works in my versions python2.7, python3.3 both when run on command line

    python3.3 p.py
    python2.7 p.py

    and as direct input

    python3.3 < p.py
    python2.7 < p.py

    My only explanations are that you've either got such an old version of python that it doesn't have the set datatype, you didn't copy and paste the code correctly, or you retyped the program wrong. The character `@' appears in 8 places. On which did you find failure?
    Last edited by b49P23TIvg; February 4th, 2014 at 08:24 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo