필터 지우기
필터 지우기

Validation of a date with a given format

조회 수: 25 (최근 30일)
Julián Francisco
Julián Francisco 2011년 4월 14일
Hi. I am designing a GUI. I have an edit text box where I enter a date string with the following format '31 Mar 2011 10:00:00.000'. I need code to validate it and write an error message in case of invalid input. Thank you for your attention. Cheers.

채택된 답변

Julián Francisco
Julián Francisco 2011년 4월 15일
Thank you all for the answers. This is one of the posible callback function codes corresponding to the edit text box that it can be used:
user_entry = get(hObject,'string');
control = regexp(user_entry,'[0-3]\d (Jan|Feb|Mar|...|Dec) \d\d\d\d (0\d|1[0-2]):[0-5]\d:[0-5]\d.\d\d\d')
if(numel(control)==0)
errordlg('Invalid Input Format','Error Message','modal')
uicontrol(hObject)
end

추가 답변 (3개)

Matt Fig
Matt Fig 2011년 4월 14일
try
D = datenum(Inpt,'dd mmm yyyy HH:MM:SS.FFF')
catch
error('Incorrect Format')
end
  댓글 수: 4
Julián Francisco
Julián Francisco 2011년 4월 15일
@Matt Fig: Thank you for your answer. Excuse me for being pedantic. It was not my intention. Cheers.
Matt Fig
Matt Fig 2011년 4월 15일
I wasn't saying you _were_ pedantic, only that as far as I know, you _might_ want to the user to enter in exactly 3 values for the milliseconds position. If you don't care about this, then the code should work fine if the user enters only one value. All else will be checked as you requested....

댓글을 달려면 로그인하십시오.


Matt Tearle
Matt Tearle 2011년 4월 14일
fmt = 'dd mmm yyyy HH:MM:SS.FFF';
dt = [];
while isempty(dt)
x = input(['enter date string in ',fmt,' format: '],'s');
try
dt = datenum(x,fmt);
catch
disp(['Hey idiot, I said ',fmt,' format'])
end
end
(Obviously I'm using input in place of the GUI entry, but the basic structure/flow would be the same)
  댓글 수: 1
Julián Francisco
Julián Francisco 2011년 4월 15일
Thank you for your answer. I grateful your intention of giving the first response. Cheers.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 4월 14일
% 31 Mar 2011 10:00:00.000
Basic validation:
regexp(t, '[0-3]\d (Jan|Feb|Mar|...|Dec) \d\d\d\d (0\d|1[0-2]):[0-5]\d:[0-5]\d.\d\d\d')
Once you've done the basic validation, you can cross-check for consistency such as the maximum day number being appropriate for the month.
  댓글 수: 3
Julián Francisco
Julián Francisco 2011년 4월 15일
@Walter Roberson: Thank you for your answer. It looks like to be the shortest one but it needs adittional code to show an error message. Cheers.
Julián Francisco
Julián Francisco 2011년 4월 15일
@Jan Simon: Thank you for your suggestion. Cheers.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by