How to Validate RegEx in MaskedTextBox

Editors for WPF Forum

Posted 2 years ago by David Mullin
Version: 17.1.0611
Platform: .NET 4.5
Environment: Windows 10 (64-bit)

Our application allows for user-defined "masks".  Currently, if the user enters an invalid RegEx for the "mask", then when this value is used at runtime (setting the Mask property of the MaskedTextBox control), it generates an exception that (because it's done via Binding) is difficult to trap and handle gracefully.

I tried using using the .Net RegEx class to validate the expression, and I also tried using the RegEx in this URL

and while that catches some violations, there are others that are "valid" but still cause MaskedTextBox to error (for instance "[0-]").

Is there any way to determine that a given RegEx is valid for the MaskedTextBox control without having to do things on the UI thread?

I've determined that I can do this code

	var text = new MaskedTextBox()
	text.Mask = myMask;
	return true;
	// Gracefully handle failure
	return false;

but that requires me to do stuff on the UI thread, which interfers with my MVVM implementation (not to mention the fact that the library with the ViewModel doesn't draw a reference on any UI assemblies).

Is there a better way?

David Mullin
IMA Technologies

Comments (2)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA

Hi David,

At the current time that might be the only way to validate it.  We could add a public static ValidateMask method on MaskedTextBox if you'd like, which could return true or false.  Or would you rather have it just throw an exception if there is a problem so that you can capture the error message?  Let us know if you'd like us to add that.

Either way though, that code would be in our Editors assembly and thus would still require a reference to the our UI assemblies.  All our custom regular expression engine logic is implemented in the Editors assembly, and while it's similar to the .NET regex engine, it does use a subset of the features.

Actipro Software Support

Posted 2 years ago by David Mullin

That enhancement might be nice for some future version.  I've got a workaround for now.



The latest build of this product (v2019.1 build 0681) was released 17 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.