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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reg exp sample

Status
Not open for further replies.

4345487

Programmer
Joined
Dec 31, 2002
Messages
132
Location
US


Hi,

I'm tryng to write a regular expresion that will check that a string has a lentgh of a least 8 and must contain one digit or special character but so far I've had no luck any suggestions or sample will be appreciated

Thanks
 
Best bet is to actually use TWO regular expressions though yes it could be done in one.
My reasoning? You will OFTEN want to use regular expressions and being able to use a small library of commonly used functions beats the heck out of having to custom write them for every unusual purpose.

This function takes in the value to test and the minimum and maximum length it should be. This way you can set a fixed lenght by passing the min and max of the same length or set a range for it to be.
Code:
function set_length(value, min, max) {
  //Test number of characters in value against min and max values.  If no value set for min it defaults 0 if no value for max it defaults to the length of value.
  var vmin=(min == '')?0:min;
  var vmax=(max == '')?value.length:max;
  var re=((value.length < vmin) || (value.length > vmax))?false:true;
  return re;
}

Alternatively you can just check the .length property to see how long it is.

This tests that at least one number is in the string.
Code:
function has_number(val) {
  if (val.search(/(0-9)+/)>-1)
    return true;
  else
    return false;
}


It's hard to think outside the box when I'm trapped in a cubicle.
 
I suppose hereafter special character be "=" to fix the idea.

[1] Well, if you accept two-step approach, that is simple---we all know it is.
[tt]
var s="abc=def";
alert(s.length<=8 && /[\d|=]/.test(s));
[/tt]
[2] If you want to play with only one pattern, then it is not impossible.
[tt]
var sspecialChar="=";
var score="[\\d|"+sspecialChar+"]";
var ssep;
var spattern="^(";
for (var i=0;i<8-1;i++) {
ssep=(i==7-1)?")$":"|";
spattern+=".{"+i+"}"+score+".{0,"+(7-i)+"}"+ssep;
}
var s="abc=def";
var rx=new RegExp(spattern);
alert(rx.test(s));
[/tt]
[3] If you want to make a big(-ger) deal.
[tt]
function RegExpx() {
this.lengthLimit=0;
this.specialChar="";
this.test=function(s) {
if (this.specialChar=="" || this.specialChar.length>1) {
return -2; //-2 for special exception: no test is performed
} else {
var spattern="[\\d|"+this.specialChar+"]";
var rx=new RegExp(spattern);
return s.length<=this.lengthLimit && rx.test(s);
}
}
}
var rxx=new RegExpx;
rxx.lengthLimit=8;
rxx.specialChar="=";
var s="abc=def";
alert(rxx.test(s));
[/tt]
and you can generalize this, taking out limitations, to unrecognizable.

All those artifacts mask the two steps approach that some may think less elegant. It does not bother me. Your call!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top