[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 821
  • Last Modified:

Regular expression to accept alpha numeric, underscore and accented characters

I have the following ASP/VBScript function that returns true if the given string contains alpha numeric characters or an underscore.

Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^\w*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window


I would like to extend the regular expression so it also allows accented characters such as áéíóúÁÉÍÓÚ etc.

Currently this function returns false if I pass the following value:
abcáéíóú

What I do know is that I would like to allow the unicode range u00C8 to u00D6 but I do not know how to add this to the current regular expression.
0
mike99c
Asked:
mike99c
  • 5
  • 4
1 Solution
 
käµfm³d 👽Commented:
Try it this way:
Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^[\w\xC8-\xD6]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

0
 
mike99cAuthor Commented:
Hi kaufmed,

I just tested your solution and it does not work as it returns false for accented characters.

I have however modifed the script with a new regular expression to add the unicode range mentioned and it now works.

Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^\w[u00C0-\u00FC]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

0
 
käµfm³d 👽Commented:
Didn't you and I already have this discussion  = )
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mike99cAuthor Commented:
Need to partially accept the solution from kaufmed.
0
 
mike99cAuthor Commented:
Solution was assisted from a previous one submitted by kaufmed so will partially accept.
0
 
mike99cAuthor Commented:
I had researched the previous question and it was my mistake not to have added the link in my original question. I thought I would simplify the whole thing by giving a clue as to what I wanted to add rather than confuse the issue by linking to another solution. I will know better next time.
0
 
käµfm³d 👽Commented:
@Qlemo

Excuse me sir, but I was not intending to pander for points. I was merely making a comment that I thought he had already asked something similar. Had I been pandering, I would have selected "Object" vs. "Post."

As point of fact, I recalled answering the question I linked to in the post above, but I couldn't find it at the time. This is why the solution I posted was inaccurate. I just happened to find the previous solution after I searched for both mine and mike99c's handles.

I am fine with the original resolution of the asker in closing the question without delegating points as my solution was inaccurate. I do ask the courtesy of not being accused of something that I don't feel I gave any hint of doing.

Thank you.
0
 
käµfm³d 👽Commented:
Vielen Dank.
0
 
mike99cAuthor Commented:
For closure, I would like to correct the script as follows:

 
Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^[\w\u00C0-\u00FC]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now