Regex Formatting Rule

Hi all

   I need to inforce a formatting rule with Regex/Javascript. I need my users to follow this basic pattern:
   Three Alpha Numerics space dash space 3 to 4 AlphaNumerics, then optionally space dash space no more than Five Alpha Numerics space dash space AnyNumber AlphaNumerics. e.g ABN - CLF [- ABFT - OPP]  (Brackets indicate optional, not part of Rule)

   Can some one help me oout, i thought i could do it but i need to be better at Regex.
thanks

   
theMightyThor1212Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GwynforWebCommented:
try this

<script>
reg=/^[a-z0-9]{3,4} - [a-z0-9]{3,4}( - [a-z0-9]{1,5} - [a-z0-9]*)?$/i

//trues
str="ABN - CLF"
alert(str+' '+reg.test(str))
str="ABN - CLF - ABFT - OPP"
alert(str+' '+reg.test(str))
str="ABN - CLF - ABFT - OPPhgh123"
alert(str+' '+reg.test(str))

//falses
str="AB? - CLF"
alert(str+' '+reg.test(str))
str="ABNRTRTRT - CLF"
alert(str+' '+reg.test(str))
</script>

theMightyThor1212Author Commented:
Thanks for responding. If i use Capitol Letters that will only allow Capitols is that correct? Also i can't seem to get this script to work if there are additional characters after the first Eight characters (i.e. ABN - CLD). Nothing after those character seem to work. Any suggestions?
My example is ABN - CLD - EAB - CLJ errors saying it is not the proper format.
TIA
bendaltonCommented:
Try this:

<script>
function testIt(testString){
if (testString.match(/^(\w{3} - \w{3}){1}( - \w{0,5} - \w*)?/)) {
//It matches
return true;
} else {
//It doesn't
return false;
}
}
</script>

example usage:

exampleString1 = "aaa - bbb - cccc - ddd";
alert(exampleString1 +" matches??? "+testIt(exampleString1))
exampleString2 = "aaaa - bbb - cccc - ddd";
alert(exampleString2 +" matches??? "+testIt(exampleString2))

Hope this helps!
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

bendaltonCommented:
quick modification to my post,  (had it matching 0-5, not 1-5)

<script>
function testIt(testString){
if (testString.match(/^(\w{3} - \w{3}){1}( - \w{1,5} - \w*)?/)) {
//It matches
return true;
} else {
//It doesn't
return false;
}
}
</script>
theMightyThor1212Author Commented:
Bendalton

  This Regex Allows White Space where it shouldn't the Basic Format Has to be as follows:
   Three AlphaNumerics Space Dash Space 3-5 AlphaNumerics[ Space Dash Space 3-5 AlphaNumerics Space Dash Space 3-9 AlphaNumerics]
With Item in [] Optional. Can i Replace user \w with [A-Z,0-9]? Ya think  that would work?
bendaltonCommented:
testString.match(/^([A-Z0-9]{3} - [A-Z0-9]{3}){1}( - [A-Z0-9]{0,5} - [A-Z0-9]*)?/)

This should work!
good catch on the whitespace
bendaltonCommented:
testString.match(/^([A-Z0-9]{3} - [A-Z0-9]{3}){1}( - [A-Z0-9]{1,5} - [A-Z0-9]*)?/)

that 0,5 should have been a 1,5 (like above)
theMightyThor1212Author Commented:
See i caught that!!!! I'm learnen, I'm Learnen Heee Hee
theMightyThor1212Author Commented:
In the Middle of this Expression, You have {1}, Does this Mean One Space? Like GwynForWebs Formula, it doesnt seem to catch the Optional Part very well, I can Still put Special Characters in it. It also does not enforce the Space Dash Space Rule in the oprional Area of the Formula
TIA
bendaltonCommented:
testString.match(/^([A-Z0-9]{3} - [A-Z0-9]{3}){1}( - [A-Z0-9]{1,5} - [A-Z0-9]*)?/)


the {1} indicates that ([A-Z0-9]{3} - [A-Z0-9]{3}) <- is only to be matched one time
( - [A-Z0-9]{1,5} - [A-Z0-9]*)?  broken down says:

match *SPACE**DASH*SPACE* literally, any character A-Z,0-9 1-5 times, *SPACE**DASH*SPACE* literally, any character A-Z,0-9 3 times <- then match this whole string 0-1 times (?)

I hope that all makes sense... if not, let me know!


theMightyThor1212Author Commented:
Hi that makes alot of Sense, thanks for explaining. The First Part ofthe Regex Works Great, the Optional Part does not. If i Enter something like this:
ABN - GRN - D&F# - ADLT

   It doesnt catch the illegal character. I need to be able to catch anything other than A-Z, 0-9 in the optional area like it does for the First Section of the Regex
bendaltonCommented:
testString.match(/^([A-Z0-9]{3} - [A-Z0-9]{3}){1}( - [^ -]{1,5} - [A-Z0-9]*)?/)


[^ -] matches every normal character but the space and the -

let me know if you need more help
theMightyThor1212Author Commented:
Maybe I am saying this the wrong way? That Last Half of the REGEX Should not match anything, If it is There, but A-Z,0-9. What you gave me is not working for the laster half of the Regex.
bendaltonCommented:
wow...this thing is gettting to be a bit more complex
.match(/(([A-Z0-9]{3} - [A-Z0-9]{3,4})|([A-Z0-9]{3} - [A-Z0-9]{3,4} - [A-Z0-9]{1,5} - [A-Z0-9]*))\Z/m)

try that... it should match either the xxx - xxx set OR the xxx - xxx - xx - xxxxxxx   but not both
bendaltonCommented:
hehe... here is my last revision (i hope)
.match(/((?!.)([A-Z0-9]{3} - [A-Z0-9]{3,4})|([A-Z0-9]{3} - [A-Z0-9]{3,4} - [A-Z0-9]{1,5} - [A-Z0-9]*)(?!.))/)

it matches either of those strings as long as they are not surrounded by any other characters
theMightyThor1212Author Commented:
Well I do appreciate your work on this, we're almost there.

Example String:         Explaination

ABN - CLD - SDFU - APPLED

ABN                          Must Be All Capitols, 3 Letters or Numbers Only, No spaces inbetween. Could Be Only Entry
 - CLD                       space dash space. If Used, Must Be Capitols, 3 Letters/Numbers No spaces. No Special Characters($%^&@#!'"). Optional.
 - SDFU                     space dash space.  If Used, Must Be Capitols, 3-5 Letters/Numbers No spaces. No Special Characters($%^&@#!'").  Optional.
 - APPLED                  space dash space.  If Used, Must Be Capitols, 3-9 Letters/Numbers No spaces. No Special Characters($%^&@#!'").  Optional.

Sorry this is so difficult, but we are close and you efforts are greatly appreciated
bendaltonCommented:
geeze louise this is a pain!

I think i got it though :-)

.match(/\A(([A-Z0-9]{3})( - ([A-Z0-9]{3})( - ([A-Z0-9]{3,5})( - ([A-Z0-9]{3,9}))?)?)?)$/)
theMightyThor1212Author Commented:
I swear i'll toss you another 500 points on a real easy one!!! I appreciate your work on this
theMightyThor1212Author Commented:
Thanks For you answer, I believe I figured it out. I needed anything after the first three Characters to Be Optional.
I took an Older Formula and edited it by adding Question Marks like this:

/^[A-Z0-9]{3,4}( - [A-Z0-9]{3,4})?( - [A-Z0-9]{1,5})?( - [A-Z0-9]*)?$/i;

 I left it Case insensitive becaus ei convert to Upper Case in the Insert Statement.
If you agree, Recopy this Formula so i can award you points.
bendaltonCommented:
/^[A-Z0-9]{3,4}( - [A-Z0-9]{3,4})?( - [A-Z0-9]{1,5})?( - [A-Z0-9]*)?$/i;
With this ^, it will match this
AAA - AAAAAAAAA
or
AAAA - A

Mine (modified to fit the format you are using),
/^(([A-Z0-9]{3})( - ([A-Z0-9]{3})( - ([A-Z0-9]{3,5})( - ([A-Z0-9]{3,9}))?)?)?)$/i;
has to match the first chunk (as you specified only 3 characters)

and the rest is optional, it just makes sure you have the previous chunk before you match the second soo...
it will match
AAA
AAA - AAA
AAA - AAA - AAAAA - AAAAAAAAA
but not
AAA - AAAA

get it?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
theMightyThor1212Author Commented:
I get it, but i think i need to leave it as such. This Organazation wants to be flexible to have the First Three be Mandatory and anything else Optional

they could have :
AAA
or
AAA - ADF
or
AAA - ADF - AAAA
or
AAA - ADFS - AAAA
or
AAA - ADDF - AGSHS - SHDNFT

But i will accept your answer and thank you very much for your time.


bendaltonCommented:
THanks for the points!


Just an FYI, on mine, the first WAS the only mandatory part....
the problem with yours was that the section with 9 characters would be valid as the second section....

soo... AAA - AAAAAAAA would be valid!

mine was set up like this (per your quoted instructions)

A - B - C - D

A: required, 3 characters
B: optional, 3 characters
C: optional, 3-5 characters, must follow B
D: optional 3-9 characters, must follow C

Without A, you could not have B, C, or D
without B, you could not have C or D
without C, you could not have D


ABN                          Must Be All Capitols, 3 Letters or Numbers Only, No spaces inbetween. Could Be Only Entry
 - CLD                       space dash space. If Used, Must Be Capitols, 3 Letters/Numbers No spaces. No Special Characters($%^&@#!'"). Optional.
 - SDFU                     space dash space.  If Used, Must Be Capitols, 3-5 Letters/Numbers No spaces. No Special Characters($%^&@#!'").  Optional.
 - APPLED                  space dash space.  If Used, Must Be Capitols, 3-9 Letters/Numbers No spaces. No Special Characters($%^&@#!'").  Optional.
theMightyThor1212Author Commented:
Yes

  I did notice that  and have copied your formula as an alternative should mine fail to catch what  I want.

Thanks Again!!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.