def validate_answer(event, text, v):
    text = text.strip()
    upper_text = text.upper()
    valid = False
    error_msg = ""
    if text == "" and event._dict.get("required", False):
        return (False, text, get_message(MSG_FIELD_REQUIRED, v))

    # Validate select
    if event.datatype == "select":
        # Try to match on phrase (i.e., "Yes" or "No")
        choices = format_choices(event._dict["choices"])
        if upper_text in choices:
            text = str(choices[upper_text])
            valid = True
        else:
            try:
                answer = int(text)
                if answer >= 1 and answer <= len(event._dict["choices"]):
                    valid = True
                else:
                    error_msg = get_message(MSG_CHOICE_OUT_OF_RANGE, v)
            except ValueError:
                error_msg = get_message(MSG_INVALID_CHOICE, v)

    # Validate multiselect
    elif event.datatype == "multiselect":
        choices = format_choices(event._dict["choices"])
        max_index = len(event._dict["choices"])
        proposed_answers = text.split()
        final_answers = {}

        try:
            for answer in proposed_answers:
                upper_answer = answer.upper()
                if upper_answer in choices:
                    final_answers[str(choices[upper_answer])] = ""
                else:
                    int_answer = int(answer)
                    assert int_answer >= 1 and int_answer <= max_index
                    final_answers[str(int_answer)] = ""
            text = " ".join(final_answers.keys())
            valid = True
        except Exception:
            error_msg = get_message(MSG_INVALID_CHOICE, v)

    # Validate int
    elif event.datatype == "int":
        try:
            int(text)
            valid = True
        except ValueError:
            error_msg = get_message(MSG_INVALID_INT, v)
    
    # Validate float
    elif event.datatype == "float":
        try:
            float(text)
            valid = True
        except ValueError:
            error_msg = get_message(MSG_INVALID_FLOAT, v)
    
    # Validate longint
    elif event.datatype == "longint":
        try:
            long(text)
            valid = True
        except ValueError:
            error_msg = get_message(MSG_INVALID_LONG, v)
    
    # Validate date (Format: YYYYMMDD)
    elif event.datatype == "date":
        try:
            assert len(text) == 8
            int(text)
            text = "%s-%s-%s" % (text[0:4], text[4:6], text[6:])
            parse(text)
            valid = True
        except Exception:
            error_msg = get_message(MSG_INVALID_DATE, v)

    # Validate time (Format: HHMM, 24-hour)
    elif event.datatype == "time":
        try:
            assert len(text) == 4
            hour = int(text[0:2])
            minute = int(text[2:])
            assert hour >= 0 and hour <= 23
            assert minute >= 0 and minute <= 59
            text = "%s:%s" % (hour, str(minute).zfill(2))
            valid = True
        except Exception:
            error_msg = get_message(MSG_INVALID_TIME, v)

    # Other question types pass
    else:
        valid = True

    return (valid, text, error_msg)
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/sms/handlers/form_session.py/validate_answer/original