Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-08-2002, 04:31 PM   PM User | #1
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Question Iterate thru the characters in a RegExp() Object?

Is it possible to iterate through the list of characters in a regular expression? A RegExp object looks suspiciously like a string. I wonder if it has similar properties.

I'm using a regular expression to check for invalid characters in user text input. If I hit an invalid character I want to list all the invalid characters for the user via "alert()". Rather than keep a separate string object in sync with the RegExp object (as my list of invalid characters grows over time), I want to simply display all the values defined in the regular expression.

I realize it may not be practical if I use "shorthand" such as [a..z]. Given the limited number of invalid characters, I intend to explictly list them all; such as
Code:
    var InvalidChars    = new RegExp ("\"|\\'");
TIA
RadarBob is offline   Reply With Quote
Old 07-09-2002, 02:51 AM   PM User | #2
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
Listing the particular offending characters might be more useful than a laundry list (of all of them). Also: if you're not compiling the RegExp at runtime, it's a lot neater to use a literal - escaping metacharacters can get very messy when strings are involved.

Code:
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="JavaScript">

function filter_badchars(field, ObjRegExp) {
var chr, c = 0, msg = '', pointer = '---------------->   ';
while (chr = field.value.charAt(c++))
if (ObjRegExp.test(chr)) msg += pointer + chr + '\n';
if (msg) {
msg = 'The following characters are disallowed:\n\n' + msg;
msg += '\n___________________ Please correct.';
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}

//////////////////////////////////////////////////////
var InvalidChars = /\$|\/|\?|~|%|&|\.|\||\\/;
//////////////////////////////////////////////////////

</script>
</head>
<body>
<h4>InvalidChars are: [$] [/] [?] [~] [%] [&] [.] [|] [\]</h4>
<form>
<input name="testfield" type="text">
<input type="button" value="Test" onclick="filter_badchars(testfield, InvalidChars)">
</body>
</html>
adios is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:04 PM.


Advertisement
Log in to turn off these ads.