library NameValidator {
    /**
     * @dev throws on invalid name
     * -converts uppercase to lower case
     * -cannot contain space
     * -cannot be only numbers
     * -cannot be an address (start with 0x)
     * -restricts characters to A-Z, a-z, 0-9
     * @return validated string in bytes32 format
     */
    function validate(string _input)
        internal
        pure
        returns(bytes32)
    {
        bytes memory temp = bytes(_input);
        uint256 length = temp.length;
        
        // limited to 15 characters
        require (length <= 15 && length > 0, "name must be between 1 and 15 characters");
        // cannot be an address
        if (temp[0] == 0x30) {
            require(temp[1] != 0x78, "name cannot start with 0x");
            require(temp[1] != 0x58, "name cannot start with 0X");
        }
        bool _hasNonNumber;
        for (uint256 i = 0; i < length; i++) {
            // if its uppercase A-Z
            if (temp[i] > 0x40 && temp[i] < 0x5b) {
                // convert to lower case
                temp[i] = byte(uint(temp[i]) + 32);
                // character is non number
                if (_hasNonNumber == false) {
                    _hasNonNumber = true;
                }
            } else {
                // character should be only lowercase a-z or 0-9
                require ((temp[i] > 0x60 && temp[i] < 0x7b) || (temp[i] > 0x2f && temp[i] < 0x3a), "name contains invalid characters");

                // check if character is non number 
                if (_hasNonNumber == false && (temp[i] < 0x30 || temp[i] > 0x39)) {
                    _hasNonNumber = true;    
                }
            }
        }
        require(_hasNonNumber == true, "name cannot be only numbers");
        bytes32 _ret;
        assembly {
            _ret := mload(add(temp, 32))
        }
        return (_ret);
    }
}
function validate(string _input)
    internal
    pure
    returns(bytes32)
{
    bytes memory temp = bytes(_input);
    uint256 length = temp.length;
        
    // limited to 15 characters
    require (length <= 15 && length > 0, "name must be between 1 and 15 characters");
    // cannot be an address
    if (temp[0] == 0x30) {
        require(temp[1] != 0x78, "name cannot start with 0x");
        require(temp[1] != 0x58, "name cannot start with 0X");
    }
    bool _hasNonNumber;
    for (uint256 i = 0; i < length; i++) {
        // if its uppercase A-Z
        if (temp[i] > 0x40 && temp[i] < 0x5b) {
            // convert to lower case
            temp[i] = byte(uint(temp[i]) + 32);
            // character is non number
            if (_hasNonNumber == false) {
                _hasNonNumber = true;
            }
        } else {
            // character should be only lowercase a-z or 0-9
            require ((temp[i] > 0x60 && temp[i] < 0x7b) || (temp[i] > 0x2f && temp[i] < 0x3a), "name contains invalid characters");

            // check if character is non number 
            if (_hasNonNumber == false && (temp[i] < 0x30 || temp[i] > 0x39)) {
                _hasNonNumber = true;    
            }
        }
    }
    require(_hasNonNumber == true, "name cannot be only numbers");
    bytes32 _ret;
    assembly {
        _ret := mload(add(temp, 32))
    }
    return (_ret);
}

/**
 * @dev throws on invalid name
 * -converts uppercase to lower case
 * -cannot contain space
 * -cannot be only numbers
 * -cannot be an address (start with 0x)
 * -restricts characters to A-Z, a-z, 0-9
 * @return validated string in bytes32 format
 */