View Full Version : Does a variable have a valid numeric value
02-18-2010, 03:29 PM
I am building an interface that has a few textFields on it.
Those textFields are used to let the user specify some numeric values.
Once I get those values, I apply that value to an attribute.
The problem that I have is that sometimes, the user enters something that is not a numeric value and then I have some errors in the script.
Is there any way to check if a string has valid numeric value?
I want the code to check that so a window will pop up in case that the user tries to introduce a non-valid value.
Thanks in advance
02-18-2010, 04:11 PM
Can't access maya at the mo so I can't give you an example, however, you can use the match MEL command with regular expressions to strip the numbers, then test if you're left with any empty string.
Alternatively, just use an intField so the user can only input numbers.
02-18-2010, 05:15 PM
Matt is right.. why are you using textFields to input numeric values?
lets assume you do want the user to sometimes input text instead of numbers for some reason, you've still got a potential problem.. If all you want is positive ints, then that's easy.. do as Matt suggested and use match/gmatch to check for only 0-9 characters.. however, if you want floats or negative numbers it starts to get messy..
If we assume that the characters 0-9 and - and . are all valid for floats we can't just check those, the user could enter something like:
and it would still be an erroneous input... you'll have to check that only the first character is a -, there is only 1 . and there are digits after it (if it exists!)
to grossly simplify things I'd suggest you just cast the input string to a float and see if it's non zero. yes, you'll get a warning each time, but that's not an error, and most users don't notice warnings if you print a blank line after each one! ;)
if (float(`textField -q -text myField`) != 0.0) print "valid value!";
02-18-2010, 05:39 PM
There's plenty of things like floatField just for this case which only take numbers. Just hit f1 and look through the gui commands they are pretty easy.
02-18-2010, 06:35 PM
I didn't realize that I was able to use something like intField.
It works just fine now.
02-18-2010, 06:35 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.