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!

Complecated regular expression

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Hey all I have a couple of regular expression questions that I would like to ask. I sort of asked this in the Perl forum (because I am a better perl programmer) but now I am going to ask it here.

I want to create a javascript object that will qualify (validate) data. I want to be able to concatenate 2 regular expressions,
# 1 no more than 8 charters
Code:
 (^\w{0,8})
and cannot have any white spaces
Code:
 \s
So lets say I have a variable
Code:
 var testData = "This is a test
I then run it through a regular expression
Code:
var testData = "This is a test
var regExp1 = (^\w{0,8})
var regExp2 = (\s)
if(regExp1.test(testData) && regExp2.test(testData){
     alet('Yes there was a match')
}
So this was a match because there was 8 digits and there was a space in it.

So I now have Two(2) questions for ya;
Question #1: Can I make One(1) if statement by contaminating the Two(2) regular expressions?

Question #2: I can I create a regular expression that will look to see if my variable is more than eight(8) characters and contains no white spaces.

From the 2 separate
regular expressions I want to make one(1) larger regular expression.

Thanks
-T

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
Use the | (or) character:
Code:
var regex = /^(\w{9,})|(\s}$/;
var a = "morethan8characters";
var b = "1 space";
var c = "I_work";

alert(regex.test(a));
alert(regex.test(b));
alert(regex.test(c));

You'll see that C will return false - meaning that it did not match any error criteria.

Just out of curiosity, why are you checking for the spaces anyway? If you are requiring them to enter 8 alphanumeric characters via \w{0,8} it will not accept spaces anyway so the check would be redundant.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top