1 Pages (7 items)
Validating Input String - Messages
#1 Posted: 12/6/2023 1:30:03 AM
What would be the best way to validate an input string in order to ensure it is a number and does not include any other characters?
Keep in mind that I'm referring to actual input in string form that is then converted into a number via str2num().
Keep in mind that I'm referring to actual input in string form that is then converted into a number via str2num().
#2 Posted: 12/6/2023 12:56:39 PM
Hi. This could be a way for positive numbers.
check.sm (4 KiB) downloaded 19 time(s).

Best regards.
Alvaro.
check.sm (4 KiB) downloaded 19 time(s).
Best regards.
Alvaro.
#3 Posted: 12/6/2023 6:12:00 PM
Here is a less compact solution, can be adapted to accept different characters.
(Positive/negative values can be allowed with an additional condition on the first character)
![2023-12-06 10_45_05-SMath Solver - [test_input.sm].png](/en-US/file/3AS5dy/2023-12-06-10_45_05-SMath-Solver---_test_input_sm__png)
test_input.sm (12 KiB) downloaded 23 time(s).
(Positive/negative values can be allowed with an additional condition on the first character)
test_input.sm (12 KiB) downloaded 23 time(s).
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
2 users liked this post
#4 Posted: 12/6/2023 9:54:14 PM
Signed numbers allowed. Also, compared with str2num results.
check.sm (12 KiB) downloaded 24 time(s).

Best regards.
Alvaro.
check.sm (12 KiB) downloaded 24 time(s).
Best regards.
Alvaro.
2 users liked this post
#5 Posted: 12/7/2023 3:16:07 AM
Near identical to Davide, this is how I would do it.
At least this is what I have done in QBasic long ago.
Regards
IsNumber.sm (7 KiB) downloaded 24 time(s).

At least this is what I have done in QBasic long ago.
Regards
IsNumber.sm (7 KiB) downloaded 24 time(s).
4 users liked this post
NDTM Amarasekera 12/8/2023 9:50:00 AM, sergio 12/7/2023 4:48:00 PM, Oscar Campo 12/7/2023 5:26:00 PM, Wesly Centerwall 12/8/2023 5:53:00 AM
#6 Posted: 12/8/2023 5:52:28 AM
Thanks guys for the input. All very insightful and useful!
1 Pages (7 items)
-
New Posts
-
No New Posts