...

View Full Version : Regular Expression character match exception



rLarson
07-31-2008, 06:58 PM
I've used generic regular expressions (regexp) before but have rarely needed to customize one. What I need to do should be simple:
Validate a form's input text allowing any character alpha (any case), numeric and special chars) except an asterisk and a percent.

Using the input text value of "Inc." (should test ok)
Based on reviewing a number of examples it seems all I needed was:

^[^%*]$ ...doesn't seem to test correctly
[^%*] ...also is not correct

What is it I'm missing ?
Any assistance is appreciated.
Thanks

Mikebert4
07-31-2008, 07:22 PM
try:




var rexp = new RegExp(/\*|\%/g);

var string = element.value; //the value of your input element

if(rexp.test(string))
{
//die
alert('INPUT NOT VALID!');
return false; // - add this line if you're inside a function
}



I'm not that well versed in regexp - I -think- a single pipe (|) character means 'or'.
of course, if I'm wrong, you can always use two expressions to test on..

thus:




var rexp1 = new RegExp(/\*/g);
var rexp2 = new RegExp(/\%/g);

var string = element.value; //the value of your input element

if(rexp1.test(string)||regexp2.test(string))
{
//die
alert('INPUT NOT VALID!');
return false; // - add this line if you're inside a function
}



Hope this helps!

rLarson
07-31-2008, 07:35 PM
Thanks for the quick reply...

Is it possible to do this in one single expression?...
besides possibly using it in javascript I will have need to include the regexp in a java struts validator.

Mikebert4
07-31-2008, 07:37 PM
I'm sure it is - I wrote myself a Javascript RegExp reference for this very reson... unfortunately it's at work (and hence no use to me now).

Give me 5 mins, I'll get you a regexp :p

Mikebert4
07-31-2008, 07:42 PM
Here we go...




/[*%]/g



Depressingly simple really, if your string contains either of those this expression will return TRUE.

so in context:



var rexp = new RegExp(/[%*]/g);

var string = element.value; //the value of your input element

if(rexp.test(string))
{
//die
alert('INPUT NOT VALID!');
return false; // - add this line if you're inside a function
}


ok?

rLarson
07-31-2008, 07:49 PM
Once again, thanks for the quick reply and going out of your way to help...
I have yet to test it but it looks good...
I believe using [^%*] should indicate all characters except but I'm going by what I covered earlier...

Mikebert4
07-31-2008, 07:59 PM
thats right - though it's easier to test a string for the characters you -don't- want, as opposed to checking them all for what you -do- want...

Of course, I don't know your context, so that could well work better for you. - don't forget to make it global within the tested string:




/[^%*]/g



let me know how you fare :)

Philip M
07-31-2008, 10:03 PM
/[^%*]/g



/[^%*]/g does not check that the string does not contain either of the characters % or *.
It will return true if any character is found in the string other than % or *
so: if string == "abc%*xyz" the regex returns true.
if string == "abdxyz" the regex again returns true.
and if string == "%%%%****" the regex returns false (only % and/or * and nothing but occurs in the string).

The OP requires:-
"Validate a form's input text allowing any character alpha (any case), numeric and special chars) except an asterisk and a percent".

The correct regex is
if (/\%|\*/.test(string.value))
or if you prefer:- if (/[%*]/.test(string.value))

which returns true if a % or a * character is found anywhere in the string.
Note the /g switch is not necessary as a single instance of a % or a * will cause the regex to return true.

You can reverse the logic thus:-

if (!/\%|\*/.test(string.value))
which will return true if no instance of % or * is found in the string. Again, a single instance of % or * will cause the regex to return false.

See the subtle difference:-

if (!/\%|\*/.test(string.value)) // returns true if string is abc but false if string is abc%
if (/[^%*]/.test(string.value)) // returns true if string is abc and true if string is abc%


You can test your regular expressions at: http://www.claughton.clara.net/regextester.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum