I want to make an InputField that only allows a specific date format to be entered.

I want to display a field hint in the format "dd:mm:yyyy". Once the user clicks on the input field, then the user should be able to enter date values. The user should not be able to enter anything that in not in the correct format. I require the ability to insert a separator of my choice. In the example above, my field separator is ":". The user should not be able to enter any other separator except for the one that I have given.

Can anyone help me?

  • $\begingroup$ You need to use something like InputField[Dynamic[var,(test[#])&],String,...] where test will be a test of the date string that is entered by the user to ensure that it conforms to your requirements. $\endgroup$ – Mike Honeychurch Feb 8 '13 at 7:37
  • $\begingroup$ If you are assign inputField value to one varibale,how can we validate with all day,month,year values. $\endgroup$ – subbu Feb 8 '13 at 8:05
  • $\begingroup$ If you use String type of InputField,again we have to convert string format to number format.so If you can use Number type of InputField,it does not allow strings,but it poses another problem of not allowing the specialCharacters. So that's why we have to customize our InputField. according to our needs. $\endgroup$ – subbu Feb 8 '13 at 8:24
  • $\begingroup$ I think what you want is not rigorously possible with InputField (unless Mike's answer is good for you of course). I would rather use multiple PopupMenu's. Try this. $\endgroup$ – Rolf Mertig Feb 8 '13 at 9:40

Test function:

dateCheck[date_String] := 
 StringMatchQ[date, DatePattern[{"Day", ":", "Month", ":", "Year"}]]

Input field:

InputField[Dynamic[date,If[TrueQ[dateCheck[#]], date = #, date = ""] &], String]

This gives you a variable date that is a string of the form dd:mm:yyyy. If you want to convert it to a list or a number then e.g.

DateList[{date, {"Day", ":", "Month", ":", "Year"}}]


AbsoluteTime[{date, {"Day", ":", "Month", ":", "Year"}}]
  • $\begingroup$ for validation,where I will specify the min ,max values of day,month,year lists. and also ,If you check with DatePattern ,it will useful only for dates formats only,but in some cases I have 4 and more elements like longitude:latitude:wind:pressure,that time it's not useful.... $\endgroup$ – subbu Feb 8 '13 at 9:39
  • $\begingroup$ @subbu seriously? $\endgroup$ – Mike Honeychurch Feb 8 '13 at 11:22
  • 11
    $\begingroup$ I will not waste my time on these questions anymore. You asked about dates and now you want to know about latitude, wind pressure. Are you taking the piss? $\endgroup$ – Mike Honeychurch Feb 8 '13 at 11:25
  • $\begingroup$ @MikeHoneychurch now the compressed string is naughty... $\endgroup$ – Yves Klett Feb 12 '13 at 11:00
  • $\begingroup$ @YvesKlett if you ask a question about dates and then say but it doesn't work for latitude etc. that is irritating for people who offer answers and wastes everyones time. Not having english as a first language is no excuse in this specific case since it is clear the OP knows how to communicate "latitude", longitude" etc. $\endgroup$ – Mike Honeychurch Feb 12 '13 at 21:42

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.