Hello. Is there a way to set an inputdlg box to only allow 5 characters? For instance is someone tries to type 6 character it just doesnt let them?
    14 views (last 30 days)
  
       Show older comments
    
Answers (2)
  Neha
    
 on 2 May 2023
        I understand that you want to create a dialog box containing a field which can accept a user input of fixed length. Please refer to the code given below, the function uses a while loop to keep displaying the inputdlg box until the user enters a valid input. 
The validation function checks if the input is exactly 5 characters. If the input is valid, the function exits the loop and returns the input. If the input is invalid, the function displays an error message and sets the default input to the previous value, so the user can try again.
function input = myInputDlg()
    prompt = {'Enter a 5-character string:'};
    dlgtitle = 'Input';
    dims = [1 35];
    definput = {''};
    input = '';
    while true
        answer = inputdlg(prompt, dlgtitle, dims, definput);
        if isempty(answer)
            % User clicked cancel
            return;
        end
        input = answer{1};
        if numel(input) == 5
            break;
        else
            errordlg('Input must be exactly 5 characters.','Input Error','modal');
            definput = {input};
        end
    end
end
2 Comments
  Walter Roberson
      
      
 on 2 May 2023
				This does not, however, satisfy the requirement that the inputdlg "just doesn't let them" input the 6th character.
  Scotty Mac
 on 6 Dec 2023
				I modified slightly to check if an input filename is greater than 16 characters...
            prompt = {'PLEASE ENTER AN OUTPUT FILENAME.'};
            dlgtitle = 'Output File';
            dims = [1 35];
            definput = {''};
            outputfile = '';
            while true
                answer = inputdlg(prompt, dlgtitle, dims, definput);
                if isempty(answer)
                    % User clicked cancel
                    return;
                end
                outputfile = answer{1};
                if numel(outputfile) <= 16
                    % Check if filename should be default                    
                    if (outputfile == "")
                        % Use default if empty
                        outputfile = 'default_filename';
                    end
                else
                    errordlg('Filename size is 16 characters maximum!','Input Error','modal');
                    definput = {outputfile};
                end
            end
  Walter Roberson
      
      
 on 2 May 2023
        No, inputdlg() has no provision for this.
I do not know what the options are for uifigure (app designer).
For traditional figure(), what you would need to do is use a KeyPressFcn callback on a uicontrol style 'edit'. The KeyPressFcn would be responsible for keeping track of every character press -- because inside KeyPressFcn for uicontrol, the String property is not updated until the user presses return or the control loses focus. The KeyPressFcn would update the String property for 1 to 5 characters, and would just stop updating the String property for additional characters.
In such a situation, it is recommended that the KeyPressFcn also detect backspace or delete, to permit the user to erase a character that has already been entered (in which case the callback should decrement the internal character count and remove the character from the String property.) However, going as far as supporting arrow keys and cursor positioning to support editing in the middle of the string is probably more trouble than it is worth.
0 Comments
See Also
Categories
				Find more on Performance and Memory in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


