Knowledgebase

Regex help needed

Posted by Jon-RackSRV, 12-29-2012, 06:47 AM
Hi Guys, We currently enforce some validation on our install for when a client sends an rDNS request into our system which is as follows: $validate = "/^([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/"; I believe this forces something1.something2.something3 something1/2 can be anything where as something3 is limited to text and dots only (for the TLD) if I recall correctly. The issue is, despite not being proper formatting, people often want to submit something.tld which our system currently blocks. Can anyone suggest a correct syntex that will allow for: something.tld something.something.tld something.something.something.tld ETC? Thanks

Posted by zoid, 12-29-2012, 06:53 AM
You only want to support up to the fourth level? No support for IDNs?

Posted by Jon-RackSRV, 12-29-2012, 07:01 AM
The 'etc' part was meant to suggest that the examples I gave were exactly that. in essence I need to force something.tld or greater i.e. to prevent someone entering just 1 block of text or 2 blocks of text with the latter not matching the type of chars found in a tld (e.g. non letters or dots). Does this make sense?

Posted by zoid, 12-29-2012, 08:06 AM
Alright, so the number of sub domains shouldnt be limited, but the question is still whether you want to support IDNs. Alternatively you could of course convert them first and then run your check.

Posted by gavin8or, 01-08-2013, 05:32 PM
Jon, Change $validate = "/^([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/"; (This seems overly complex/hacked on and hacked on. I'm surprised it (sorta) works) to $validate = "/^(\.[a-zA-Z0-9_-]+\.)+([a-zA-Z]{2,6})$/"; The first element (\.[a-zA-Z0-9_-]+\.) with match if there are + (meaning: one or more) of them. so "some_thing.some-thing." will match as well as "some5thing." and "1.two.3.four". The second element ([a-zA-Z]{2,6})$ is your TLD and EOL. You said that part is working anyways? My new version will validate "1.2.3.4.com" and "hi.net" and seems like it's what you want. I've checked it in bash and perl shell and should work for you.

Posted by Jon-RackSRV, 01-08-2013, 06:00 PM
Thanks Gavin, I've got it working now

Posted by zoid, 01-08-2013, 06:10 PM
I can only emphasise it (also for others who want to use it) again, keep in mind this does not cover IDNs. Last edited by zoid; 01-08-2013 at 06:14 PM.

Posted by Website themes, 01-09-2013, 01:25 PM
Technically a trailing dot is legal in a domain name. So example.com. is perfectly fine. But I don't think it will validate according to Gavin's regex. Maybe end it with a .?



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
c++ file operations (Views: 587)
JaguarPC IS Down ?? (Views: 643)


Language:

Client Login

Email

Password

Remember Me

Search