struct TrackDetails {
    string details;
    bool used;
mapping(bytes32 => TrackDetails) track;

function saveDetails(bytes32 index, string _trackDetails) public {
    require(int(index) != 0 && bytes(_trackDetails).length != 0);
    require(track[index].used != true);
    track[index].details = _trackDetails;
    track[index].used = true;

Here index is like unique id and given by the person who enters track details. But the problem is, index is accepting white spaces. How to avoid this?

  • What do you mean when you say that index is accepting white spaces? From the declaration index is a bytes32, so it is a string of 32 bytes, or 64 hexadecimal characters. – Ismael Jan 10 '18 at 4:35
  • if I am simply entering a white space in quotes it takes, but I want to avoid that because an id can never be a white space. Also I need to retrieve data using that id. – sruthi23 Jan 10 '18 at 6:59
  • As MiguelMota says in his answer making validations is solidity is expensive with little to gain. You are better making those validation in the client side. – Ismael Jan 10 '18 at 16:12

Validation in solidity is hard and very costly.

You can simplify all the checks you're trying to do by simply making the keccak256 of the index be the unique key for the data.


contract MyContract {
    mapping(bytes32 => string) public track;

    function saveDetails(bytes32 index, string _trackDetails) public {
        bytes32 key = keccak256(index);
        require(keccak256(track[key]) != keccak256(""));
        track[key] = _trackDetails;

Better yet, generate the index keys on the contract itself and map them to the sender.


contract MyContract {        
    mapping(int256 => string) public track;
    mapping(address => int256) public trackOwner;
    int256 sequenceId;

    function makeTrack() public {
        trackOwner[msg.sender] = sequenceId;

    function saveDetails(string _trackDetails) public {
        require(trackOwner[msg.sender] != 0);
        track[trackOwner[msg.sender]] = _trackDetails;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.