In building an assignment for my class, I would like set up an InputField, where they will enter a function. I was wondering if there was a way to "continuously" check if their function is being defined correctly (the thing is, they often forget the underscore after the variables as in f[x_], they use ( ) instead of [ ], etc.).

I am imagining some red "X" that becomes a green checkmark when the beginning of the definition is correctly written (similar to the way some websites show you if you've re-entered your password correctly when signing up for an account).

If this seems too complicated, I would even be interested in seeing how to mask something simpler, like perhaps a phone number.

Is this possible in Mathematica?


  • $\begingroup$ Mathematica is quite difficult to parse "continuously". Think for example in this (convoluted) way of defining a function. (With[{# = f[x_]}, argtra := Sin[x]]) &@argtra $\endgroup$ – Dr. belisarius Oct 17 '12 at 19:00
  • 3
    $\begingroup$ In this answer, I show a way to create an input field that evaluates continuously and displays the result (True/False) for each key stroke. You can modify that to your needs and make it a red X and a green check instead of T/F. $\endgroup$ – rm -rf Oct 17 '12 at 19:04
  • $\begingroup$ @rm-rf: While I like halirutan's solution below, your answer linked above is very cool, and I will definitely be using it as well! Thanks! $\endgroup$ – Steve D Oct 18 '12 at 0:52

I want to give an answer using SyntaxQ and SyntaxLength. SyntaxQ takes a string and returns True, when the string has correct Mathematica syntax. On the other hand SyntaxLength returns an integer which shows up to what character the string is correct syntax. If the integer is larger than the length of the input string it indicates, that more input is needed. The following function uses this to create a colored Style-text showing where errors occur, whether more input is needed or whether the string is correct syntax:

parseString[str_String] := 
  With[{l = SyntaxLength[str], t = SyntaxQ[str], 
    len = StringLength[str]},
    Style[str, 14, Darker[Green]],
    l > len,
    Row[{Style[str, 14], Style["_", 14, Red]}],
    Row[{Style[StringTake[str, Min[l, StringLength[str]]], 14], 
      Style[StringDrop[str, Min[l, StringLength[str]]], 14, Red]}]
parseString[__] := "";

Now I use this to continuously check an input field

Panel[Column@{InputField[Dynamic[str], String, 
    ContinuousAction -> True], Dynamic[parseString[str]]}]

Test cases

Mathematica graphics

Mathematica graphics

Mathematica graphics

  • $\begingroup$ Wow, this is great! $\endgroup$ – Steve D Oct 18 '12 at 0:49
  • $\begingroup$ @Steve, thanks. $\endgroup$ – halirutan Oct 18 '12 at 0:52
  • $\begingroup$ @halirutan. I, too, think this is a great answer, but I think it's a little misleading to say your function "creates a colored string" since it returns either a Style object or a Row object containing Style objects. These are objects that Mathematica displays as styled text. I think it is useful to make this distinction because in the past I got myself into trouble thinking I could treat Style objects as strings. $\endgroup$ – m_goldberg Oct 18 '12 at 1:29
  • 1
    $\begingroup$ @m_goldberg I fixed it. Hopefully the people using this see that it's not one Style but a Row.. Btw, I never mind if someone improves my text! $\endgroup$ – halirutan Oct 18 '12 at 1:32

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.