Validating Input String

Validating Input String - Messages

#1 Posted: 12/6/2023 1:30:03 AM
Wesly Centerwall

Wesly Centerwall

26 likes in 156 posts.

Group: User

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().
#2 Posted: 12/6/2023 12:56:39 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Hi. This could be a way for positive numbers.

check.sm (4 KiB) downloaded 19 time(s).

check.png

Best regards.
Alvaro.
#3 Posted: 12/6/2023 6:12:00 PM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

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
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
sergio 12/7/2023 4:48:00 PM, Wesly Centerwall 12/8/2023 5:53:00 AM
#4 Posted: 12/6/2023 9:54:14 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Signed numbers allowed. Also, compared with str2num results.

check.sm (12 KiB) downloaded 24 time(s).

check.png

Best regards.
Alvaro.
2 users liked this post
Wesly Centerwall 12/8/2023 5:53:00 AM, sergio 12/7/2023 4:48:00 PM
#5 Posted: 12/7/2023 3:16:07 AM
overlord

overlord

547 likes in 1330 posts.

Group: Moderator

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).

2023-12-06_22-12.png
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
Wesly Centerwall

Wesly Centerwall

26 likes in 156 posts.

Group: User

Thanks guys for the input. All very insightful and useful!
#7 Posted: 12/9/2023 3:55:37 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Hi. Parenthesis added. Notice that the plus/minus sign must to be only at the first's places, but with parenthesis it is not more true. And it looks like SMath's str2num have a bug: str2num(" ((1)" ) is wrong.

Check.sm (21 KiB) downloaded 22 time(s).

check.png

Best regards.
  • New Posts New Posts
  • No New Posts No New Posts