Click to See Complete Forum and Search --> : Regular Expression...


Timg
07-24-2003, 01:18 PM
I've working on a little form field character filter. The following script alters the background of every form field that matches the regular expression.

In reality, I need it the regular expression to do almost exactly the opposite.

The requirements are as follows...

Valid characters
A-Z
a-z
0-9
#
.
?
*
- (I know this one is not in the current reg exp)
\s

Any field containing characters other than those listed under valid characters is incorrect and should have it's background color changed.

Currently the script changes fields were the valid characters exist. I did this with the intention of just trying to make the regular expression match the inverse of the valid characters. I've been toying with this for a good four hours now (it now accepts spaces!) and I'm hoping someone with a little regular expression know how might be able to shed some light on the problem.


<html>
<head>
<SCRIPT language="javascript">
function checkFields(){
var pattern = new RegExp("(\s{0,}([A-Za-z0-9*?.#]+)\s{0,})+");
var form = document.forms[0];
var focusField = null;
for(var i = 0; i < form.length; i++){
var element = form.elements[i];
if(element.type == "text"){
if(element.value.search(pattern) != -1){
alert("match on " + i);
if(focusField == null) focusField = element;
element.style.background = "#D06868";
}
else {
element.style.background = "";
}
}
}
if(focusField != null) focusField.focus();

}

</SCRIPT>
</head>
<body>
<form>
<input type="text" name="blah"><br/>
<input type="text" name="blah1"><br/>
<input type="text" name="blah2"><br/>
<input type="text" name="blah3"><br/>
<button onclick="checkFields()">check</button>
</form>

</body>
</html>


Tim

Jona
07-24-2003, 01:41 PM
Is this what you wanted?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
<head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT type="text/javascript">
<!--
function checkFields(){
var pattern = new RegExp("(\s{0,}([A-Za-z0-9*?.#\-]+)\s{0,})+");
var form = document.forms[0];
var focusField = null;
for(var i = 0; i < form.length; i++){
var element = form.elements[i];
if(element.type == "text"){
if(!pattern.exec(element.value)){
alert("Match on " + i);
if(focusField == null) focusField = element;
element.style.backgroundColor = "#D06868";
}
else {
element.style.backgroundColor = ""; }
} }
if(focusField != null) focusField.focus();
}
//-->
</SCRIPT>
</head>
<body>
<form action=""><div>
<input type="text" name="blah"><br>
<input type="text" name="blah1"><br>
<input type="text" name="blah2"><br>
<input type="text" name="blah3"><br>
<button onclick="checkFields()">check</button>
</div></form>
</body>
</html>


[J]ona

Exuro
07-24-2003, 01:47 PM
Well, looks like Jona beat me to it :(... But I attached my code anyway.

Edit:
Actually, Jona's doesn't accept spaces. I donno what the heck is with that RexExp... Looks really messy... I don't think he edited yours :-p. But anyway, mine lets you use spaces and it seems to be working ok...

Edit Again:
Ah, I see what's going on now. Jona didn't change your regexp, and he also left your code so that it highlighted valid text boxes instead of invalid ones. So if you just stuck a ! at the beginning of that IF statement it'd work fine

Jona
07-24-2003, 01:49 PM
Originally posted by Exuro
Well, looks like Jona beat me to it :(... But I attached my code anyway.

Well, I sat here staring at the forum for about ten minutes. When I saw no one had replied, I decided to go ahead and answer his question. Also, two things about your code. First, there is no attribute "LANGUAGE" for the <SCRIPT> tag, only TYPE. Second, I forgot to use #FFFFFF on the last one (I left it blank). So... You helped, too. :)

[J]ona

Exuro
07-24-2003, 01:57 PM
Yeah, I read about how the Language attribite can mess things up, even just having it there. I was using HtmlKit to write that code and it has a little Auto-Complete thing where I type in "<script " and then it does the whole:

<script language="JavaScript" type="text/javascript">
<!--

//-->
</script>

I've been thinking that I need to take the language out of there, but I hadn't bothered yet. I guess since someone called me on it I should probably go do it now :-p.

Jona
07-24-2003, 01:59 PM
lol. I just thought I'd mention it. (I will say nothing of your other HTML errors.) :D

[J]ona

Timg
07-24-2003, 02:00 PM
Thanks for the attempt. I should have made my self a little more clear. (and maybe I should find a regular expression forum, but thought I'd give it a shot here first)

I'm not looking to invert my if statement.

I'm looking for a regular expression that matches everything not contained in this particular regular expression.

Basically, it works like this. If a field contains a character that is not in the list of characters I provided earlier, that field is invalid. I can't finish the regular expression.

Currently, this regular expression produces the following results.

hfjdshfkjsdhf - match
hdsajk djask - match
ANDJdsajk 78 - match
321 ?#*. DfsU - match
; - not match
hfj; -match (this is a problem as semi-colon is not to be matched)
$jsdajhFIJBKF -not match
-match (all spaces or no input)


It's closer and I maybe able to go from here, but it doesn't allow for empty now.


So what I'm asking for is regular expression help to help me fix the matching problems. Then I'll get to inverting the logic.


Tim

P.S. sorry for sounding like a whiny kid, but I got numerous hours in this prob.

Jona
07-24-2003, 02:07 PM
Originally posted by Timg
Thanks for the attempt. I should have made my self a little more clear. (and maybe I should find a regular expression forum, but thought I'd give it a shot here first)

Well, this is a JavaScript question, and I don't think I've ever seen any Regular Expressions fora (forums) before...

Originally posted by Timg
I'm looking for a regular expression that matches everything not contained in this particular regular expression. Basically, it works like this. If a field contains a character that is not in the list of characters I provided earlier, that field is invalid. I can't finish the regular expression.

I got lost on block A. :p Too much negation. (If this doesn't not do this, it doesn't not work?) You want the RegEx to match everything that it does right now, and if it finds that character, it's valid? Isn't that what it does?

Originally posted by Timg
Currently, this regular expression produces the following results.

Uh... I'm guessing you want it to work so that the semi-colon will be detected at the end, as well? Is that all your question was originally?

[J]ona

Exuro
07-24-2003, 02:09 PM
Uhm... I'm pretty sure that what I posted does that, doesn't it? My RegExp was:

/^[A-Za-z0-9\#\.\?\*\-\s]+$/

So if the input was anything other than what you listed (A-Z,a-z,0-9,#,.,?,*,-,\s) then it would be invalid and not match. That's what you wanted, right?

Edit:
Oh wait, were you wanting it to match anything OTHER than those characters? Try this then:

/^[^A-Za-z0-9\#\.\?\*\-\s]+$/

And then un-invert the logic

Edit Again!:
Agh! I'm screwing up left & right on this! The regexp you'd be wanted would be this:

/[^A-Za-z0-9\#\.\?\*\-\s]+/;

Jona
07-24-2003, 02:11 PM
Originally posted by Exuro
Uhm... I'm pretty sure that what I posted does that, doesn't it? My RegExp was:

/^[A-Za-z0-9\#\.\?\*\-\s]+$/

So if the input was anything other than what you listed (A-Z,a-z,0-9,#,.,?,*,-,\s) then it would be invalid and not match. That's what you wanted, right?

Mustn't forget the semi-colon. Maybe he wants that in there, too.

[J]ona

Timg
07-24-2003, 02:14 PM
Hmm....

It's a character filter. All characters in the list are valid. Anything character entered into a form field must be in the valid list or the field is incorrect.

So if I type letters (upper or lower), numbers, pound, period, qmark, asterisk, dash, and any white space character in any combination, the form field is fine. And all other characters error out no matter where they appear in the value of the text input.

Exuro's reg expression is almost there. It doesn't allow blank fields. Which I think I can pull off.

Tim

Timg
07-24-2003, 02:15 PM
Sorry for the hubub guys.

You got it. I'm just goin mad or something...

Thanks,
Tim

Something about a forest... All I saw were the trees.