Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all,
I am validating an email with the pattern like
JavaScript
var pattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/;


and I am testing for a match like this...
JavaScript
if (!r.test(txtEmail.Text))


it works fine...
but if I get this pattern value from resource file like
JavaScript
var pattern= "<%= Resources.abcResources.patterntText %>";

the value assigned to the variable pattern is
JavaScript
var pattern = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/";

as a result, the matching at
JavaScript
if (!r.test(txtEmail.Text))

fails. The error says "Object doesnot supports property or method 'test'"
so how can I remove these quotes and test for a match?
Posted
Updated 30-Apr-13 20:46pm
v2

Well, don't add quotes at first place:
JavaScript
var pattern= <%= Resources.abcResources.patterntText %>;

The <%= %> tag is evaluated on place.
 
Share this answer
 
v3
Comments
Naz_Firdouse 1-May-13 2:51am    
yes I tried the same approach and its working fine...
It shows error at semicolon but still its working fine...
thanks
Zoltán Zörgő 1-May-13 6:21am    
Check syntax. But the idea is this one.
Naz_Firdouse 1-May-13 6:38am    
I am able to match the values...
if I use like this
var pattern= <%= Resources.abcResources.patterntText %>
var s = "Hello";
it shows a syntax error at var s...
but the project builds and works as per the requierment
Zoltán Zörgő 1-May-13 15:27pm    
I have missed a semicolon at the end, but you missed it too:
var pattern= <%= Resources.abcResources.patterntText %>;
Naz_Firdouse 2-May-13 9:43am    
No...I used in my app...
but it shows a red mark at semicolon...
and the app builds successfully...
So I have a doubt why it is showing a red mark and build succeeds???
Another alternative syntax is
JavaScript
var pattern = new RegExp("<%= Resources.abcResources.patterntText %>");

It strongly types the variable as a RegExp. Your original code
JavaScript
var pattern= "<%= Resources.abcResources.patterntText %>";

was interpreted as a string rather than a RegExp. And using the RegExp constructor avoids the VS validation error caused by this code:
JavaScript
var pattern = <%= Resources.abcResources.patterntText %>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900