0

Is there a simple way or function to check if the entry to the Editbox is numeric including float numbers. Any alphabetic or alphanumeric entries will not be allowed. Without float number part I would check ascii but I think it does not work for float numbers.

Thanks

user2937812
  • 49
  • 2
  • 8
  • [boost::lexical_cast](http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/examples.html) – anatolyg Nov 06 '13 at 16:31
  • 1
    without requiring boost you could control what keys are allowed by monitoring the keypressed events as detailed at http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=VS.71).aspx – GMasucci Nov 06 '13 at 16:38
  • @GMasucci This question is tagged C++. Not C++/CLI. – IInspectable Nov 06 '13 at 19:53

2 Answers2

2

Since your question is tagged with mfc, here is a code snippet using CString:

CString ss;
float ff;
GetDlgItemText(IDC_MY_EDIT_BOX, ss);
if (_stscanf_s(ss, _T("%f"), &ff) == 1)
    // ff contains the value
else
    // error

If you need to use a double precision number, use "%lf" in the scanf call.

EDIT:

CString ss;
float ff;
GetDlgItemText(IDC_MY_EDIT_BOX, ss);
LPCTSTR lpszString = ss;
TCHAR *endptr;
ff = (float) _tcstod(lpszString, &endptr);
if (endptr != lpszString && *endptr == '\0')
    // ff contains the value
else
    // error
Edward Clements
  • 5,040
  • 2
  • 21
  • 27
  • This results in false positives. The input `1 abc` will be interpreted as a successful conversion, even though the input does not meet the requirements. – IInspectable Nov 06 '13 at 19:23
1

If it is a dialog you can add an edit control to the Dialog. Than Launch the Dialog wizard add a variable to this edit control. Choose type float. The DDX_Text Routine will do the rest.

But this will permit entry of alphabetic chars. If you want to fix this too. You can subclass the edit control with a Special OnChar (WM_CHAR) handler that only allows decimal numbers and the decimal point.

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • Doing this will allow to enter **any** character into the edit box. Validation is not performed as characters are entered, but rather when the dialog is dismissed using the OK button. Maybe this is what you meant to say. It's also worth noting that validation can be further customized by using the `DDV_*` functions (like [`DDV_MinMaxDouble`](http://msdn.microsoft.com/en-US/LIBRARY/xec6fc26.aspx)). – IInspectable Nov 06 '13 at 19:18
  • @xMRi thanks for the response. But I already defined variable for that editbox and used in many places. Still is there a way to apply your method ? – user2937812 Nov 06 '13 at 19:33
  • @IInspectable: I wrote about this fact and if there is a restriction needed, subclassing the the way to solve it.... – xMRi Nov 07 '13 at 06:48
  • @user2937812: Just add your own DDX_ or DDV_ handler or replace the existing DDV_/DDX_ handler. Everything that is done per wizard can be done easily by hand ;) – xMRi Nov 07 '13 at 06:49
  • @xMRi You wrote *"this will not permit entry of alphabetic chars"* which is quite the opposite of the behavior I described. My description is correct; you may want to update your answer. – IInspectable Nov 07 '13 at 08:39
  • @IInspectable: You got me. Sorry English isn't my native tongue, so excuse the fault. I just fixed this in my answer! – xMRi Nov 07 '13 at 12:24