Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
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 – Dr. belisarius Oct 17 '12 at 19:00
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. – R. M. Oct 17 '12 at 19:04
@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! – Steve D Oct 18 '12 at 0:52
up vote 10 down vote accepted

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

share|improve this answer
Wow, this is great! – Steve D Oct 18 '12 at 0:49
@Steve, thanks. – halirutan Oct 18 '12 at 0:52
@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. – m_goldberg Oct 18 '12 at 1:29
@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! – halirutan Oct 18 '12 at 1:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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