as no one can help me in the WebForms-Forum maybe it was the wrong Place to ask,
so i try it here again. I'm sure someone can tell me something about this Problem, or am I the first person who tried this !?
...we build a own TexBox wich creates Validators at runtime,
now we want to use a 'RegularExpressionValidator' because we have allready a Regular Expression in our c#-Code for validating values after postback.
The Validation after Postback works fine with 'RegEx'.
The Problem is that the Regular Expression is for example like '\d\w', so that in the c#-Code ist must be '\\d\\w' (Escaping).
If we set the 'ValidationExpression' of the 'RegularExpressionValidator' at runtime like
'ValidationExpression = regularExpression'
then the ValidationExpression on the HTML-Souce is '\\d\\w' wich does not work, couse is had to be '\d\w'.
So I think this is a bug in the rendering of the RegularExpressionValidator.
As the "validation expert" here, I've read your posting several times previously in the other forum and couldn't come up with an answer. Here are the thoughts I had that may help:
1. In C#, use the string syntax @"text". This will allow you to enter the string without escaping: @"\d\w"
2. In Javascript, it is necessary to use the \\ to represent a single \ in a string. (It works like C#.) So I would have thought that a string of "\\d\\w" would have been read into a javascript string variable and treated as the
literal text "\d\w". If this is the case, then the ValidationExpression="\\d\\w" attribute in the <span> tag of the validator is correct.
Now if you are only looking for two characters, a digit and letter, javascript's regex works a little differently from VS.NET. With JavaScript, you should be sure you use the ^ and $ symbols: "^\d\w$". (These symbols work fine in .net but don't seem as important
to its parser.)
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
I've used Regular Expression validators several times and would recommend the use of the "@" as Peter has suggested. This keeps the code quite clean. As for the expression itself trust Mr. Blum he is the expert. His controls are quite amazing!
first thanks for your replies.
But that don't solve the problem,
the '\d\w' was a simple example, this is a real expression genrated by my textbox:
'^-?[0-9]{0,3}(,[0-9]{0,2})?$'
this is automaticly genreated for a number with maximum 3 integer digits and 2 fraction digits,
this works fine because i used '[0-9]' instead of '\d'
of corse it works if i enter the expresseion in the designer with one backslash, but this is no solution because the expression must be generated at runtime.
Using the @-symbol leads to the same as entering the strings excaped, i see it in the debugger the strings escaped on on the page (in the validator) they are also escaped.
I added the expression to the text in the texbox for testing, and there it apears without the escape slashes!
Andre, I think your right (for asp.net 2.0). Even the built-in expressions are wrong. Setting the ValidationExpression to @"^-?\d{0,3}(,\d{0,2})?$" produces the following javascript expression: "^-?\\d{0,3}(,\\d{0,2})?$". Even though the javascript expression
is not equivalent to the server side version, it's still a valid expression and should trigger the validator when the associated controls value doesn't produce a match!? It appears that client side validation is completely disabled.
So I guess I'm seconding Andre's question. Is there some change in the behavior of the RegularExpressionValidator we don't know about? Some setting somewhere we've missed?
In ASP.NET 2.0, a property value like ValidationExpression is written differently. It used to appear embedded into the validator's <span> tag. Now its shown in javascript near the end of the form. This is required for XHTML compatibility because XHTML does
not permit the concept of "expando properties".
then the code is using javascript strings instead of HTML attributes. By definition when you have a string in javascript where you want to use the \ character, you must have \\. Its just like C# that way.
So the above expression, with \\, is valid as the string is assigned to the RegExp object. In fact, I do this all of the time in my own validation code. Its just a necessity. So I have to wonder if the expression is incorrect. Try a simple test that validates
only digits:
^\d+$
That will become ^\\d+$ in the javascript string but should be treated as the original when assigned to the RegExp object.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Doh! You're right of course. I wonder why they didn't use a regular expression literal? Anyway, I tried my test again and everything worked perfectly. The regex is evaluated on the client as expected (as opposed to earlier when I couldn't get it to work
at all). I must have had something else on the page that broke the validation framework.
Andre77: I'm stumped. What you're doing should work.
yes you are right it works, I'm a bit confused but it realy works now.
I think i dont change anything.
First i tried your example (\d) ind the designer, of course it works, thenn i tried it in codebehind and expected it should not work, but it did.
Now i uncomment my automitic genration code and it works also!
Maybe it was somthing else disturbing the validator last time i tried it....
Andre77
Member
30 Points
9 Posts
RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Feb 23, 2006 05:57 AM|LINK
as no one can help me in the WebForms-Forum maybe it was the wrong Place to ask,
so i try it here again. I'm sure someone can tell me something about this Problem, or am I the first person who tried this !?
...we build a own TexBox wich creates Validators at runtime,
now we want to use a 'RegularExpressionValidator' because we have allready a Regular Expression in our c#-Code for validating values after postback.
The Validation after Postback works fine with 'RegEx'.
The Problem is that the Regular Expression is for example like '\d\w', so that in the c#-Code ist must be '\\d\\w' (Escaping).
If we set the 'ValidationExpression' of the 'RegularExpressionValidator' at runtime like
'ValidationExpression = regularExpression'
then the ValidationExpression on the HTML-Souce is '\\d\\w' wich does not work, couse is had to be '\d\w'.
So I think this is a bug in the rendering of the RegularExpressionValidator.
Some Ideas how to get it work?
Thanks for reading
Andre
rmack005
Member
135 Points
28 Posts
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Feb 23, 2006 02:48 PM|LINK
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Feb 23, 2006 03:03 PM|LINK
Hi Andre,
As the "validation expert" here, I've read your posting several times previously in the other forum and couldn't come up with an answer. Here are the thoughts I had that may help:
1. In C#, use the string syntax @"text". This will allow you to enter the string without escaping: @"\d\w"
2. In Javascript, it is necessary to use the \\ to represent a single \ in a string. (It works like C#.) So I would have thought that a string of "\\d\\w" would have been read into a javascript string variable and treated as the literal text "\d\w". If this is the case, then the ValidationExpression="\\d\\w" attribute in the <span> tag of the validator is correct.
Now if you are only looking for two characters, a digit and letter, javascript's regex works a little differently from VS.NET. With JavaScript, you should be sure you use the ^ and $ symbols: "^\d\w$". (These symbols work fine in .net but don't seem as important to its parser.)
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
dbottjer
Contributor
2410 Points
466 Posts
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Feb 24, 2006 01:56 AM|LINK
I've used Regular Expression validators several times and would recommend the use of the "@" as Peter has suggested. This keeps the code quite clean. As for the expression itself trust Mr. Blum he is the expert. His controls are quite amazing!
ThriveFast | Blog | Follow Me @dbottjer
Please "Mark As Answer" if helped
Andre77
Member
30 Points
9 Posts
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Feb 27, 2006 10:48 AM|LINK
first thanks for your replies.
But that don't solve the problem,
the '\d\w' was a simple example, this is a real expression genrated by my textbox:
'^-?[0-9]{0,3}(,[0-9]{0,2})?$'
this is automaticly genreated for a number with maximum 3 integer digits and 2 fraction digits,
this works fine because i used '[0-9]' instead of '\d'
of corse it works if i enter the expresseion in the designer with one backslash, but this is no solution because the expression must be generated at runtime.
Using the @-symbol leads to the same as entering the strings excaped, i see it in the debugger the strings escaped on on the page (in the validator) they are also escaped.
I added the expression to the text in the texbox for testing, and there it apears without the escape slashes!
THX
Andre
rmack005
Member
135 Points
28 Posts
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Feb 27, 2006 06:11 PM|LINK
Andre, I think your right (for asp.net 2.0). Even the built-in expressions are wrong. Setting the ValidationExpression to @"^-?\d{0,3}(,\d{0,2})?$" produces the following javascript expression: "^-?\\d{0,3}(,\\d{0,2})?$". Even though the javascript expression is not equivalent to the server side version, it's still a valid expression and should trigger the validator when the associated controls value doesn't produce a match!? It appears that client side validation is completely disabled.
So I guess I'm seconding Andre's question. Is there some change in the behavior of the RegularExpressionValidator we don't know about? Some setting somewhere we've missed?
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Feb 28, 2006 04:21 PM|LINK
Just a theory after reading these facts.
In ASP.NET 2.0, a property value like ValidationExpression is written differently. It used to appear embedded into the validator's <span> tag. Now its shown in javascript near the end of the form. This is required for XHTML compatibility because XHTML does not permit the concept of "expando properties".
So if you are reading code like this:
RegularExpressionValidator1.ValidationExpression = "^-?\\d{0,3}(,\\d{0,2})?$"
then the code is using javascript strings instead of HTML attributes. By definition when you have a string in javascript where you want to use the \ character, you must have \\. Its just like C# that way.
So the above expression, with \\, is valid as the string is assigned to the RegExp object. In fact, I do this all of the time in my own validation code. Its just a necessity. So I have to wonder if the expression is incorrect. Try a simple test that validates only digits:
^\d+$
That will become ^\\d+$ in the javascript string but should be treated as the original when assigned to the RegExp object.
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
rmack005
Member
135 Points
28 Posts
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Mar 01, 2006 02:54 PM|LINK
Doh! You're right of course. I wonder why they didn't use a regular expression literal? Anyway, I tried my test again and everything worked perfectly. The regex is evaluated on the client as expected (as opposed to earlier when I couldn't get it to work at all). I must have had something else on the page that broke the validation framework.
Andre77: I'm stumped. What you're doing should work.
Andre77
Member
30 Points
9 Posts
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Mar 02, 2006 05:58 AM|LINK
yes you are right it works, I'm a bit confused but it realy works now.
I think i dont change anything.
First i tried your example (\d) ind the designer, of course it works, thenn i tried it in codebehind and expected it should not work, but it did.
Now i uncomment my automitic genration code and it works also!
Maybe it was somthing else disturbing the validator last time i tried it....
Thank you very much for your help!
Andre
dhaval_upadh...
Member
6 Points
3 Posts
Re: RegularExpressionValidator - dynamic ValidationExpression in CodeBehind (c#)
Aug 10, 2009 07:30 AM|LINK
Hello friends,
I have the same problem here but not solved for me.
I tried for \\ and also @, ^ but still not working.
I dont know what is wrong.
When I write the ValidationExpression in design time t is working.
Here I am sending my code.
int CountryCode = Convert.ToInt32(ddlCountry.SelectedItem.Value);
switch (CountryCode)
{
case 229: //US
RegExpPostalCode.ValidationExpression = @"\d{5}(-\d{4})?";
break;
case 102:// India
RegExpPostalCode.ValidationExpression = "^[1-9]{3}\\s{0,1}[0-9]{3}$";
break;
case 44://China
RegExpPostalCode.ValidationExpression = @"[0-9]{5,}$";
break;
case 75://france
RegExpPostalCode.ValidationExpression = @"\d{5}";
break;
case 210://Switzerland
RegExpPostalCode.ValidationExpression = @"[1-9]{1}[0-9]{3}$";
break;
case 13://Aus
RegExpPostalCode.ValidationExpression = @"(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$";
break;
case 82: //Germany
RegExpPostalCode.ValidationExpression = @"(D-)?\d{5}";
break;
case 228: //UK
RegExpPostalCode.ValidationExpression = @"(([A-Z]{1,2}[0-9][0-9A-Z]{0,1})\ ([0-9][A-Z]{2}))|(GIR\ 0AA)$";
break;
case 152: Netherlands
RegExpPostalCode.ValidationExpression = @"\b[1-9]\d{3}\ +[A-Z]{2}\b";
break;
case 111: //Japan
RegExpPostalCode.ValidationExpression = @"\d{3}(-(\d{4}|\d{2}))?";
break;
default:
RegExpPostalCode.ValidationExpression = "";
break;
}