Rank: Advanced Member Groups: Registered
Joined: 02/04/2018(UTC) Posts: 135 Was thanked: 22 time(s) in 19 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
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 28/08/2014(UTC) Posts: 1,368 Was thanked: 827 time(s) in 524 post(s)
|
Hi. This could be a way for positive numbers. check.sm (5kb) downloaded 1 time(s).Best regards. Alvaro. Edited by user 07 December 2023 03:52:35(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,654 Location: Italy Was thanked: 1340 time(s) in 879 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) 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 |
2 users thanked Davide Carpi for this useful post.
|
on 07/12/2023(UTC), on 08/12/2023(UTC)
|
|
Rank: Advanced Member Groups: Registered
Joined: 28/08/2014(UTC) Posts: 1,368 Was thanked: 827 time(s) in 524 post(s)
|
Signed numbers allowed. Also, compared with str2num results. check.sm (13kb) downloaded 4 time(s).Best regards. Alvaro.
|
2 users thanked Razonar for this useful post.
|
on 07/12/2023(UTC), on 08/12/2023(UTC)
|
|
Rank: Advanced Member Groups: Registered
Joined: 23/07/2013(UTC) Posts: 1,129 Was thanked: 510 time(s) in 340 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).
|
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)
|
|
Rank: Advanced Member Groups: Registered
Joined: 02/04/2018(UTC) Posts: 135 Was thanked: 22 time(s) in 19 post(s)
|
Thanks guys for the input. All very insightful and useful!
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 28/08/2014(UTC) Posts: 1,368 Was thanked: 827 time(s) in 524 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).Best regards.
|
|
|
|
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.