Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline wb.c  
#1 Posted : 06 December 2023 07:30:03(UTC)
wb.c


Rank: Advanced Member

Groups: Registered
Joined: 02/04/2018(UTC)
Posts: 149
United States

Was thanked: 25 time(s) in 21 post(s)
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().

Edited by user 09 December 2023 05:18:59(UTC)  | Reason: Not specified

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline Razonar  
#2 Posted : 06 December 2023 18:56:39(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,387
Uruguay

Was thanked: 841 time(s) in 533 post(s)
Hi. This could be a way for positive numbers.

check.sm (5kb) downloaded 1 time(s).

check.png

Best regards.
Alvaro.

Edited by user 07 December 2023 03:52:35(UTC)  | Reason: Not specified

Offline Davide Carpi  
#3 Posted : 07 December 2023 00:12:00(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,662
Man
Italy
Location: Italy

Was thanked: 1348 time(s) in 884 post(s)
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 (13kb) downloaded 5 time(s).

Edited by user 07 December 2023 02:40:27(UTC)  | Reason: Not specified

If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects
thanks 2 users thanked Davide Carpi for this useful post.
on 07/12/2023(UTC),  on 08/12/2023(UTC)
Offline Razonar  
#4 Posted : 07 December 2023 03:54:14(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,387
Uruguay

Was thanked: 841 time(s) in 533 post(s)
Signed numbers allowed. Also, compared with str2num results.

check.sm (13kb) downloaded 4 time(s).

check.png

Best regards.
Alvaro.
thanks 2 users thanked Razonar for this useful post.
on 07/12/2023(UTC),  on 08/12/2023(UTC)
Offline overlord  
#5 Posted : 07 December 2023 09:16:07(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 1,133
Turkey

Was thanked: 515 time(s) in 344 post(s)
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 (8kb) downloaded 4 time(s).

2023-12-06_22-12.png
thanks 4 users thanked overlord for this useful post.
on 07/12/2023(UTC),  on 07/12/2023(UTC),  on 08/12/2023(UTC),  on 08/12/2023(UTC)
Offline wb.c  
#6 Posted : 08 December 2023 11:52:28(UTC)
wb.c


Rank: Advanced Member

Groups: Registered
Joined: 02/04/2018(UTC)
Posts: 149
United States

Was thanked: 25 time(s) in 21 post(s)
Thanks guys for the input. All very insightful and useful!
Offline Razonar  
#7 Posted : 09 December 2023 09:55:37(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,387
Uruguay

Was thanked: 841 time(s) in 533 post(s)
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 (22kb) downloaded 3 time(s).

check.png

Best regards.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.