DaNuGai Posted June 25, 2009 Share Posted June 25, 2009 I am looking for a regular expression for "Domain\UserName"...so essentially string "Text1\Text2" should return as valid. Minor condition is "text1" should have a max length of 8.Thanks. Link to comment Share on other sites More sharing options...
boen_robot Posted June 25, 2009 Share Posted June 25, 2009 [A-Za-z0-9-_]{1,8}\\[A-Za-z0-9-_]+ This says one to eighth alphanumeric characters or a "-" or "_", followed by a "\", followed by one or more alphanumeric characters or a "-" or "_"If it doesn't work (as I'm a little uncertain about the "-" and "_"), try this as well:([A-Za-z0-9]|\-|\_){1,8}\\([A-Za-z0-9]|\-|\_)+ Just curious, we are talking about NetBIOS domain names, right? Link to comment Share on other sites More sharing options...
DaNuGai Posted June 25, 2009 Author Share Posted June 25, 2009 Thanks for your reply. By the way, the regular expression you gave me doesn't seem to work properly.If the first string is longer than 8 characters, it still returns true.Also, I am only want to use alpha characters (no Numberic and no Special characters). I tried something like ([A-Za-z]){1,8}\\([A-Za-z])+ and it doesnt' work as well.Thanks again for your help. I appreciate it. [A-Za-z0-9-_]{1,8}\\[A-Za-z0-9-_]+ This saysIf it doesn't work (as I'm a little uncertain about the "-" and "_"), try this as well: ([A-Za-z0-9]|\-|\_){1,8}\\([A-Za-z0-9]|\-|\_)+ Just curious, we are talking about NetBIOS domain names, right? Link to comment Share on other sites More sharing options...
boen_robot Posted June 25, 2009 Share Posted June 25, 2009 Considering the fact we're talking about JavaScript, I have to ask... are you sure you've written the string correctly? You need to also escape the "\" in it, like so: var regex = /^[A-Za-z]{1,8}\\[A-Za-z]+$/;var valString1 = "this\\valid";var valString2 = "domain\\thisIsAVeryLongUserName";var valString3 = "d\\t";var invString1 = "thisCouldBeValidIfOnlyMoreThanEighthCharactersWereAllowed\\thisToo";var invString2 = "domain\\\\ThisWasGoingToBeValidIfTwoSlashesWereAcceptable";var invString3 = "\\ThisWasGoingToBeValidIfTheDomainCouldBeEmpty";var invString4 = "domain\\";//Despite being a valid NetBIOS name, as per your request, the below is not acceptedvar invString5 = "my-server\\user";//both should be truealert(regex.test(valString1));alert(regex.test(valString2));alert(regex.test(valString3));//all three should be falsealert(regex.test(invString1));alert(regex.test(invString2));alert(regex.test(invString3));alert(regex.test(invString4));alert(regex.test(invString5)); Link to comment Share on other sites More sharing options...
DaNuGai Posted June 25, 2009 Author Share Posted June 25, 2009 Thanks for your quick reply.I don't mean to be a pain but I am using the following site to test out the regular expressions and I am not getting the same results.Regular Expression Validator Link to comment Share on other sites More sharing options...
justsomeguy Posted June 25, 2009 Share Posted June 25, 2009 Those regex aren't running in Javascript, he's probably using VB for that. That might be the difference. Link to comment Share on other sites More sharing options...
DaNuGai Posted June 25, 2009 Author Share Posted June 25, 2009 That's what I thought too but I am getting the same results on the following website as well...Validator Link to comment Share on other sites More sharing options...
justsomeguy Posted June 25, 2009 Share Posted June 25, 2009 It's missing the start and end.^([A-Za-z]){1,8}\\([A-Za-z])+$^ means to match from the start of the string, and $ means to match until the end. The reason it was matching without that is because when you had a string like this:askldfjklasdjfklasjdf/askldfjdit was matching only this part:fklasjdf/askldfjdWith the start and end there, it means that it needs to match the entire string, not just part of it. Link to comment Share on other sites More sharing options...
DaNuGai Posted June 26, 2009 Author Share Posted June 26, 2009 Thank you so much JustSomeGuy I appreciate your help. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 26, 2009 Share Posted June 26, 2009 Hey no problem. If you check post 4 you'll see that boen_robot wrote the same pattern in his test script, with the start and end. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.