Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple RegEx

Status
Not open for further replies.

simonchristieis

Programmer
Jan 10, 2002
1,144
GB
I am using a regular expression validator to determine whether a form is posted.

What I want to do is detect when the dropdown value is NOT matched in the reg ex.

Anyone know how to do this ?

Code:
^[Select Make]
 
Using the following kind of syntax:
Code:
var myRegExp = /^\[Select Make\]$/i;

alert(myRegExp.test("Toyota Corolla")); // false
alert(myRegExp.test("")); // false
alert(myRegExp.test("[Select Make]")); // true
Now insert some [!]![/!] to reverse things:
Code:
var myRegExp = /^\[Select Make\]$/i;

alert([!]![/!]myRegExp.test("Toyota Corolla")); // true
alert([!]![/!]myRegExp.test("")); // true
alert([!]![/!]myRegExp.test("[Select Make]")); // false
I'm sure you follow how to adapt it for your validation routine.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Thanks - the problem is I am using this within a regular expression validation control, so cannot reverse things outside the regEx.

So I need to regEx to say, yep, this is fine because it doesn't match this string.

 
This is how you can do it efficiently. You first divid the string to be checked upon into 3 particles. The middle part be what you reject (negatively match). The first and last particles are what you positively match. Let's say they are "xyz" and "abc" case-sensitive to be concrete. The algorithm is this.
[tt]
function checkit(s) { //s be the dropdown value or whatever
var p_1,p_2,rx_1,rx_2;

p_1=["^(.*?)","(\\[Select Make\\])","(.*?)$"];
p_2=["^(xyz)","(.*?)","(abc)$"];

rx_1=new RegExp(p_1.join("")); //case-sensitive, else add flag
rx_2=new RegExp(p_2.join("")); //case-sensitive, else add flag

return !rx_1.test(s) && rx_2.test(s);
}
[/tt]
 
Thanks again, but, I need a single expression for my validation control.

All I can pass in is a simgle expression and the control to validate.
 
>I need a single expression for my validation control.
Yes? You don't want to see it.

>All I can pass in is a simgle expression and the control to validate.
If you understand the approach, you would pass twice each single expression.
 
If lookahead/lookbehind is within the range of acceptable expression, you have use negative lookahead. (There is an asymmetry in the support of lookahead and lookbehind in js engine you normally encounter in browser. You'll have practically sure chance in lookahead.)
[tt]
function checkit2(s) {
var rx=new RegExp("^(xyz)(?!\\[Select Make\\])(.*?)(abc)$"); //case-sensitive, else add flag
return rx.test(s);
}
[/tt]
Particles (xyz) and (abc) are what explained in my prevous post to make it concrete. Replace them to whatever you've now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top