当前位置: 动力学知识库 > 问答 > 编程问答 >

Validation Error in Lightswitch with RegEx

问题描述:

I have a property that is NIF, and is like SSN but in Spain and the format can be:

A0000000A

00000000A

A00000000

Where A stands for Alphanumeric and 0 for Digits. In any case it must be 9 characters.

Using Visual Studio 2013 LightSwitch I try to validate it using RegEx.

The regular expression I wrote for this case is

^\b\w\d{7}\w\b$

and I have tested in several webs, and so far, the RegEx works.

But when I run the app put a valid data it doesn't work and the validation error appears on screen.

Here is the validation code:

Private Sub NIF_Validate(results As EntityValidationResultsBuilder)

Dim pattern As String = "^\b\w\d{7}\w\b$"

If (NIF IsNot Nothing) AndAlso (Not Regex.IsMatch(pattern, NIF)) Then

results.AddPropertyError("Check NIF")

End If

End Sub

网友答案:

The backslash is the escape character in strings. Therefore you need to define the pattern string as

"^\\b\\w\\d{7}\\w\\b$"

whereby \\b respectively \b is here twice not really needed because of ^ and $.

The regular expression string defined by your Visual Basic code as interpreted is ^bwd{7}wb$.

网友答案:

Finally, after many hours reviewing the code I found the error. Its in Regex.IsMatch where the first parameter must be the text and the second one the pattern.

Regex.IsMatch(input As String, pattern As String)

Thanks for the answers. And sorry for the silly mistake.

分享给朋友:
您可能感兴趣的文章:
随机阅读: