ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Validating Input Box (https://www.excelbanter.com/excel-programming/378053-validating-input-box.html)

Satish

Validating Input Box
 
Hi All

Is there any easy way I can validate the string entered by the user in
an input box?

I basically don't want the user to enter characters that are considered
invalid for file/folder names. i.e., \ / : * ? " < |

Will I have to parse the string and check each character one by one or
is there any easy way to do it?

Ideas appreciated.

Thanks
Satish


NickHK

Validating Input Box
 
Satish,
Using InputBox, you can't do anything until the user click OK and returns
the strings. Then you can test it.

If you make your own pseudo InputBox with a userform/textbox etc, you can
monitor the characters as typed in the KeyDown/KeyPress events and react
accordingly.

NickHK

"Satish" wrote in message
oups.com...
Hi All

Is there any easy way I can validate the string entered by the user in
an input box?

I basically don't want the user to enter characters that are considered
invalid for file/folder names. i.e., \ / : * ? " < |

Will I have to parse the string and check each character one by one or
is there any easy way to do it?

Ideas appreciated.

Thanks
Satish




Joerg

Validating Input Box
 
Look into Regular Expressions. Makes parsing relatively easy. Here is a
quick example that allows only alphanumeric characters and period (code
needs refinement, but I think it gives you an idea)

Sub ValidateInput()
Dim RegExp As Object
Dim inputstr, re
Set RegExp = CreateObject("VBScript.RegExp")
Set re = RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "[^a-z0-9\.]" 'Matching pattern are characters that are NOT
(indicated by the ^ sign) within a-z, 0-9 or the period character.

inputstr = InputBox("Input your string")

Do While re.test(inputstr) = True
inputstr = InputBox("Please correct your input")
Loop

MsgBox "Correct/corrected input: " & inputstr
End Sub

Cheers,

Joerg


"Satish" wrote in message
oups.com...
Hi All

Is there any easy way I can validate the string entered by the user in
an input box?

I basically don't want the user to enter characters that are considered
invalid for file/folder names. i.e., \ / : * ? " < |

Will I have to parse the string and check each character one by one or
is there any easy way to do it?

Ideas appreciated.

Thanks
Satish





All times are GMT +1. The time now is 05:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com