Web tier: servlets, JSP, Web frameworks: Validating domain name with struts validator

  1. Can anyone help me write proper validation for validating a domain name (only SLD), I am trying few things wit RegEx but guess I am not very comfortable with regular expressions now. The conditions for valid domain mae are as follow - * Domain Name must be composed of the letters a through z, the numbers 0 through 9, and the hyphen (-) character. * The Domain Name must not begin or end with the hyphen character. * The Domain Name must not contain spaces. * The Domain Name must not contain special characters other than the hyphen character. * The Domain Name must contain 2 to 63 characters, inclusive. * Domain Names are not case sensitive. What should I put in this mask now considering the conditions mentioned above? mask ???????? TIA, - Manish
  2. The following regexp will cover everything but the length check, which can be done by adding minlength and maxlength clauses to the validation rule (Covered in the validator docs). ^([a-zA-z0-9]{2,})(-([a-zA-Z0-9]+))*$
  3. Thanks!!! It almost works perfect, but it takes an uderscore as a vaild inpits which is incorrect. Besides alpha-numeric characters, I only want hyphen to be accepted. - Manish
  4. Hmmm, Oops, I made a slight typing mistake in the original regular expression - the second 'z' should be uppercase. The typo means that anything between uppercase 'A' and lowercase 'z' will be allowed in the section before the first '.' - and this would include any of the following characters... [\]^_` My apologies for any extra time that you wasted looking for my typo. Just for the record, here is a link to a great tool that allows you to do 'what-if' experiment with regular expressions, this really helps you to practice using them - something that you should be doing because they really do make you code easier to read (and adjust) http://www.weitz.de/regex-coach/ Enjoy!
  5. Thanks a ton for the useful link! :) - Manish