-2

In the VBScript code, I am trying to disallowed Non-English characters. I have created a below function to validate the input value:

Function Valid(ByVal value) 

        'instantiate regex object container, output boolean '
        Dim objRegEx, retVal 

        'using late binding, VBScript reference is not required '
        Set objRegEx = CreateObject("VBScript.RegExp") 

        '.pattern 
        With objRegEx 
              .Pattern = "/[^\x00-\x7F]+/" 
              .IgnoreCase = True 
              .Global = True
        End With 

        retVal = objRegEx.Test(value) 
        
        'get rid of RegEx object '
        Set objRegEx = Nothing 

        Valid = retVal 

 End Function

But /[^\x00-\x7F]+/ expressions is not working.

What expressions do I need to add to validate the string?

amaze
  • 7
  • 5
  • @Lankymart, I already tried but did not worked – amaze Aug 26 '20 at 11:03
  • 2
    You need `.Pattern = "[^\x00-\x7F]" `. If you mean any non-English letter, that will be much harder (i.e. if you only need to match alphabetic chars other than ASCII) – Wiktor Stribiżew Aug 26 '20 at 11:57
  • Please define exactly what did you mean by non english letter ? Can you give us an example ? And what do you expect as result ? – Hackoo Aug 26 '20 at 12:13
  • @WiktorStribiżew I tried with `.Pattern = "[^\x00-\x7F]" `.. Not working. – amaze Aug 26 '20 at 12:19
  • 1
    @Hackoo, I have a TEXT file where sometimes we get non-English characters (i.e â ä è ê Ç ?½). Want to validate the data and alert the error to user to update the TEXT file by correcting to English characters – amaze Aug 26 '20 at 12:21
  • @amaze Just saying it did not work, isn't helpful. – user692942 Aug 26 '20 at 13:14
  • @amaze You say it's not working but you are testing for the existence of the characters which would mean the string isn't valid (I'm guessing) so you want to invert the boolean result of the `Test()` method surely? At the moment if the test passes `Valid()` will return `True` which I'm guessing you expect to be `False`. Just add `Not` i.e `retVal = Not objRegEx.Test(value)` or you could invert the RegEx pattern. – user692942 Aug 26 '20 at 13:16
  • 1
    @Lankymart, Yes I have already inverted the boolean result and it's worked. I forgot to inform you here. Thank you so much for your help! – amaze Aug 26 '20 at 14:57
  • @amaze Please check my edit answer ! – Hackoo Aug 26 '20 at 15:14
  • 1
    Yeah, checkout @Hackoo's answer it's just a regurgitation of mine and Wiktor's comments. – user692942 Aug 26 '20 at 15:36
  • @amaze Please take Stack Overflow (SO) [tour](https://stackoverflow.com/tour) to learn how to say thanks on SO by accepting an answer on being helpful by clicking on gray check mark symbol left to an answer. See also [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/) A question with no accepted answer is rated as a not answered question ! Thank you for reading ! – Hackoo Aug 26 '20 at 17:07

1 Answers1

0

As he said @Wiktor Stribiżew in his comment, You need to validate a pattern like this [^\x00-\x7F]

See Demo Here


Option Explicit
Dim Title,MyInput
Title = "Check the validity of the input"
Do
    MyInput = InputBox("Type anything here to check its validity ",Title,"â ? è ê ½")
    If MyInput = "" Then Wscript.Quit
    If ValidationTest(MyInput) = True Then
        MsgBox DblQuote(MyInput) &" is a valid input",64,Title
    else
        MsgBox DblQuote(MyInput) &" is not a valid input",16,Title
    End If
Loop Until ValidationTest(MyInput) = True
'----------------------------------------------------
Function ValidationTest(strString)
    Dim RegularExpressionObject
    Set RegularExpressionObject = New RegExp
    With RegularExpressionObject
        .Pattern = "[^\x00-\x7F]"
        .IgnoreCase = True
        If Not .Test(strString)= True then
            ValidationTest = True
        end if
    End With
End Function
'----------------------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'----------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70