Valid Input Type


Rachel

Recommended Posts

I'm writing a sequence to check whether the user inputted a valid input in the edit box component. I want it to be an integer. When I do strtodouble("123abc"), I get 123 instead of NaN. Which function should I use to get NaN or to figure out that the input does indeed have text in it?

Link to comment
Share on other sites

DAQFactory follows the C / C++ standard libraries and accepts numeric values up to the first invalid character.  It will only return NaN() if the first character is invalid.  The workaround depends on what are acceptable numbers.  In most cases, you can just convert the result of strToDouble back to a string and see if it is the same as what was entered.  So:

private string datain = "123abc"

valid = doubleToStr(strToDouble(datain)) == datain

The only issue with this is that if someone enters: "123.40" it will think it is incorrect, because doubleToStr() won't add the trailing 0.  

If this is a problem, you'll probably have to write your own little script to validate the input.  Probably the easiest way then would be to use the findOneOf() function and just include every invalid character.  Something like (I'm not going to include every character):

valid = findOneOf(datain, "abcdefghijklmnopqrstuvwxyz!@# etc...") == -1

But that won't deal with someone entering something like "123.40.56"

Short of you writing your own algorithm for this, I'm guessing you could use a regular expression and therefore the FindExpr() function.  I'm not sure what the regular expression would be for validating a number, but regular expressions have been around for decades so I'm guessing a quick Internet search will give you some useful ones.

Link to comment
Share on other sites

I see your point, thanks! However, I'm having trouble obtaining the original value from the edit box component. For example, suppose my box is called userInput.  I type "123abc" into the box, and I run private string datain = userInput. Printing datain gives me "123" instead of "123abc". Is there a way to access this "123abc" raw input before evaluation to "123"?

Link to comment
Share on other sites

I'm not sure what you are doing.  You have an on screen edit box?  You named the component "userInput" or do you have the edit box changing the value of userInput.  I am guessing the second, and that you have userInput declared as a number.  So, the edit box is converting 123abc to a number so it can be put into userInput, thus getting 123, then you are putting that result into your datain string variable and getting "123".

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.