Question : How can I validate a string?

Hi,
I need to make sure that I get the name of the domain that the user passes to my script via a form. I want to do all the error checking automatically instead of asking the user to reenter the domain name.
all i want is the word "domain" out of "www.domain.com" or "domain.com" or any other combination of a domain that the user can type into my input box. note that they might put http:// or ftp or ftp:// or even something like htp:// which could be a typo. Please keep in mind it might ebd with other things than .com

After that, i need to make sure that the domain doesnt end with a "-" dash or start with a dash, if so, delete it and run it through a check again to make sure it passes everything.

Also, the domain must not have any other characters than letters/numbers/dashes if so, delete the character.

If the domain is longer than 22 characters, truncate to 22.

And the simplest check of all, make sure that there IS a domain entered :) If there is no domain submitted, put in the word blank as the domain. So $domain = "blank".

NOTE: make sure that if any of the checks are failed, the error gets fixed and the domain has to run through all the checks again.

$domain should be the final perfect domain string.
$in{'dom'} is the raw string passed by user.

Thanks a ton!

Answer : How can I validate a string?

Try this on for size.  I think I just forgot a "g" in one of the regex's.  Lemme know if that does the trick for ya.

      use CGI;
      $query= CGI->new();
      $host = $query->param('dom');    

      @tlds = ("com","org","edu","net","gov");
      $host =~ s/\s//g;  # Collapses spaces
      $host =~ s/^[^:]*:\/\///;  # gets rid of http:// etc.
      $host =~ s/\/.*$//;  # gets rid of path and file

      # Strips tlds
      foreach $tld (@tlds) {
          $host =~ s/(\.$tld)$//;
          last if $1;
      }

      $host =~ s/^.*\.//;  # strips off host names.

      $done = 0;

      while (! $done) {
         $done = 1;

         if(($host =~ /^\-([^\-]*)\-?$/) || ($host =~ /^([^\-]*)\-$/)) {
             $host = $1;
             $done = 0;
              print "1\n";
             next;
         } elsif($host =~ /[^a-zA-Z0-9\-\.]/) {
             $host =~ s/[^a-zA-Z0-9\-\.]//g;
             $done = 0;
              print "2\n";
              next;
         } elsif($host =~ /^$/) {
             $host = "blank";
             $done = 0;
              print "3\n";
             next;
         }

         $host = substr($host, 0, 22);
      }

      $domain = $host;
Random Solutions  
 
programming4us programming4us