/    Sign up×
Community /Pin to ProfileBookmark

Regular Expression…

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

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@JonaJul 24.2003 — [font=arial][color=maroon]Is this what you wanted?[/color][/font]

[font=monospace]

<!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>

[/font]



[b][J]ona[/b]
Copy linkTweet thisAlerts:
@ExuroJul 24.2003 — Well, looks like Jona beat me to it ?... But I attached my code anyway.

[b]Edit:[/b]

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...

[b]Edit Again:[/b]

Ah, I see what's going on now. Jona [I]didn't[/I] 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

[upl-file uuid=c93d6454-30a5-4fce-865e-bcf1083a6ad3 size=872B]regexp.txt[/upl-file]
Copy linkTweet thisAlerts:
@JonaJul 24.2003 — [i]Originally posted by Exuro [/i]

[B]Well, looks like Jona beat me to it ?... But I attached my code anyway. [/B][/QUOTE]


[font=arial][color=maroon]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. ?[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@ExuroJul 24.2003 — 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.
Copy linkTweet thisAlerts:
@JonaJul 24.2003 — [font=arial][color=maroon]lol. I just thought I'd mention it. (I will say nothing of your other HTML errors.) ?[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@TimgauthorJul 24.2003 — 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.
Copy linkTweet thisAlerts:
@JonaJul 24.2003 — [i]Originally posted by Timg [/i]

[B]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)[/b][/quote]


[font=arial][color=maroon]Well, this is a JavaScript question, and I don't think I've ever seen any Regular Expressions fora (forums) before...[/color][/font]

[i]Originally posted by Timg [/i]

[b]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.[/b][/quote]


[font=arial][color=maroon]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?[/color][/font]

[i]Originally posted by Timg [/i]

[b]Currently, this regular expression produces the following results.[/B][/QUOTE]


[font=arial][color=maroon]Uh... I'm guessing you want it to work so that the semi-colon will be detected at the end, as well? Is that [i]all[/i] your question was originally?[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@ExuroJul 24.2003 — 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?

[b]Edit:[/b]

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

[b]Edit [i]Again![/i]:[/b]

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

/[^A-Za-z0-9#.?*-s]+/;
Copy linkTweet thisAlerts:
@JonaJul 24.2003 — [i]Originally posted by Exuro [/i]

[B]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? [/B]
[/QUOTE]


[font=arial][color=maroon]Mustn't forget the semi-colon. Maybe he wants that in there, too.[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@TimgauthorJul 24.2003 — 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
Copy linkTweet thisAlerts:
@TimgauthorJul 24.2003 — 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.
×

Success!

Help @Timg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...