473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1809
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bosconian | last post by:
Using preg_replace() is there a simple regexp to strip everything from a string except alpha and numeric chars (a-zA-Z0-9)? $input = "$tring1!"; $pattern = $input = preg_replace($pattern, "",...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
6
by: Daniel McLaughlin | last post by:
I have the following code that matches just fine. var re; re = /23-Apr-2004/gi; alert(re.test(document.all.innerHTML)); However, when I attempt it with a variable containing the same...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
6
by: Edward | last post by:
I need to validate a text box entry, but ONLY if it is 17 characters, otherwise I have to ignore it. My regular expression for the validation is: ^(({9})()()(\d{6}))$ Can I adapt this to...
1
by: redryderridesagain | last post by:
Using the Microsoft VBScript Regular Expressions 5.5 library in MS-Access VBA 6.3. I understand why the code below finds an occurance of the string "PC: blah blah blah A: BBB aaa B: BBB bbb C:...
6
by: micklee74 | last post by:
hi i created a script to ask user for an input that can be a pattern right now, i use re to compile that pattern pat = re.compile(r"%s" %(userinput) ) #userinput is passed from command line...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
2
by: gypsy3001 | last post by:
Today, I was trying to construct a regular express in JavaScript. But I kept getting the "Error: unterminated parenthetical" message. I want to do something like this: keyCombo = ":(";...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.