469,328 Members | 1,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

RegExp - Build dynamic pattern

Hello: I have written an ASP.net app that pulls settings from a
database. Part of the setting definition is a regular expression that
I want to use on the client via JavaScript for data validation...
1) Sample code for the setting object:
----------------------------------------------------
function setting(id, name, validationPattern, validationMsg) {
this.id = id;
this.name = name;
this.validationPattern = validationPattern;
this.validationMsg = validationMsg;
}
2) Sample code on the page that creates a new setting object:
----------------------------------------------------
setting(1, 'test', '^\\d{1,1}$|^\\d{2,2}$|^100$', 'invalid value');
3) The issue:
----------------------------------------------------
The setting object gets built just fine except the pattern argument
gets escaped by javascript!!!

^\\d{1,1}$|^\\d{2,2}$|^100$

GETS ESCAPED TO

^\d{1,1}$|^\d{2,2}$|^100$ (Notice how the double \\ is now a single \)
Is there any way for me to preserve the pattern text as is?

Thank you,
zbig555z

Jul 23 '05 #1
2 1722
zbig555z wrote:
Hello: I have written an ASP.net app that pulls settings from a
database. Part of the setting definition is a regular expression that
I want to use on the client via JavaScript for data validation...
1) Sample code for the setting object:
----------------------------------------------------
function setting(id, name, validationPattern, validationMsg) {
this.id = id;
this.name = name;
this.validationPattern = validationPattern;
this.validationMsg = validationMsg;
}
2) Sample code on the page that creates a new setting object:
----------------------------------------------------
setting(1, 'test', '^\\d{1,1}$|^\\d{2,2}$|^100$', 'invalid value');
3) The issue:
----------------------------------------------------
The setting object gets built just fine except the pattern argument
gets escaped by javascript!!!

^\\d{1,1}$|^\\d{2,2}$|^100$
A literal js regex should look something like this:

/^\\d{1,1}$|^\\d{2,2}$|^100$/

The regex above looks rather strange, are you looking for any positive
number between "0" and "100"(inclusive)?

/(^\d\d?$)|(^100$)/
<script type="text/JavaScript">
var x="";
for(i=0;i<105;i++){
x+=i+": "+/(^\d\d?$)|(^100$)/.test(i)+"<BR>";
}
document.write(x)
</script>
A little crude, perhaps, it allows "0" and "00".
Mick

GETS ESCAPED TO

^\d{1,1}$|^\d{2,2}$|^100$ (Notice how the double \\ is now a single \)
Is there any way for me to preserve the pattern text as is?

Thank you,
zbig555z

Jul 23 '05 #2
Thanks for the reply...This worked...

the heart of the issue is that I was writing out the patterns as
strings not literal RegEx objects.

I changed

'pattern' to /pattern/ and all is good.

Thanks again!

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bosconian | last post: by
6 posts views Thread by Daniel McLaughlin | last post: by
8 posts views Thread by Dmitry Korolyov | last post: by
6 posts views Thread by Edward | last post: by
1 post views Thread by redryderridesagain | last post: by
6 posts views Thread by micklee74 | last post: by
11 posts views Thread by HopfZ | last post: by
2 posts views Thread by gypsy3001 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.