Solved

Website URL validation

Posted on 2002-03-12
13
1,029 Views
Last Modified: 2013-12-24
a quick one. i have a form where users can enter a web address. a link to their own website to be exact. users tend to enter all sorts of wrong things. so i need it to format it into the correct format.

ie:  somepeople might enter these:

1. <a href="www.mysite.com">mysite.com</a>
2. http://www.mysite.com

i need it to format that input, and insert into the database the correct info in this format:

www.mysite.com

whats the easiest or best way to do this?


0
Comment
Question by:M_Corkish
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 37

Expert Comment

by:meverest
ID: 6856242
Somthing like:

<cfset fixedurl=ReReplace("#form.url#", "[^""/]*/*([^"" ])", "/1")>
<cfoutput>#fixedurl#</cfoutput>

?

if it doesn't work (likely!), i'll need to do some more work on my regex and try again!  :-}

cheers.
0
 

Author Comment

by:M_Corkish
ID: 6860170
Hmm, it doesn't quite work properly.. it spits out "/1" quite often. if you enter:

<a href="www.mysite.com">mysite.com</a>

it brings back:

/1"www.mysite.com">mysite.com

================
here is the full page i tested it on:

<cfif isdefined("form.url")>

<cfset fixedurl=ReReplace("#form.url#", "[^""/]*/*([^"" ])", "/1")>
<cfoutput>#fixedurl#</cfoutput>

</cfif>

<form action="test.cfm" method="post">
URL:
<input type="text" name="url">
<input type="submit" value="submit">

</form
0
 

Expert Comment

by:slingle
ID: 6860369
This isn't some long logical solution, but...  What I always do is put http:// as a label in front of the input field.  99.9% of the time, this keeps the user from entering the http (or html) in the field.  That way you get only the url and you control how it goes in your db.  

example:

<p>http://<input type="text" name="url"><br>
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Expert Comment

by:slingle
ID: 6860375
well i forgot experts exchange autmatically links urls so let me try my example again:

&lt;p&gt;http://<input type="text" name="url"&gt;&lt;br&gt;
0
 

Author Comment

by:M_Corkish
ID: 6860459
yeah, i have that.. but in the first day of running the site, people got it wrong.. even after saying "http://"

some people also try to enter <A href="www.mysite.com">mysite.com</a> on other comments and messages they post? is their a good way to deal with this?

0
 
LVL 11

Accepted Solution

by:
jimmy282 earned 100 total points
ID: 6860498
This works :-)

<cfset OriginalString = "http://the-whizkids.com">

Or

<Cfset OriginalString = "<a href=""http://the-whizkids.com""></a>">

Or

<cfset OriginalString = "the-whizkids.com">

<CFSCRIPT>
       OriginalString = ReReplaceNocase(OriginalString,"<a[^h]*href=""?([^"" ]*)""?[^>]*>([^<]*)</A>",
"\2","ALL");

OriginalString = ReRePlaceNoCase(OriginalString,"^(http|ftp|https)://","");

  </CFSCRIPT>

  <cfoutput>
  #OriginalString#  
  </cfoutput>
0
 
LVL 11

Expert Comment

by:jimmy282
ID: 6860511
THIS
<Cfset OriginalString = "<a href=""http://the-whizkids.com""></a>">
should have been
<Cfset OriginalString = "<a  href=""http://the-whizkids.com"">the-whizkids.com</a>">

Jimmy
0
 

Author Comment

by:M_Corkish
ID: 6860546
Jimmy, it looks good, I changed the second example from:

<Cfset OriginalString = "<a href=""http://the-whizkids.com""></a>">

to:

<Cfset OriginalString = '<a href=""http://the-whizkids.com""></a>'>


substituting the double quotes for the single one. as it didn't work, due to the fact do body would actually enter in href=""www.blah.com"". using the single one works however in that case.

Thanks for the good stuff.  So what exact are the ReReplaceNoCase functions all about?  and the \2  for? and such?

0
 
LVL 11

Expert Comment

by:jimmy282
ID: 6860590
These are Regualr Expressions.
Perl is the langauge which handles them best and CF really handles hem very differently. They are available in all languages though. ASP,PHP,CF, Java etc.

\2 means that the second match from the pattern found by the regular expression ","<a[^h]*href=""?([^"" ]*)""?[^>]*>([^<]*)</A>" which actually is the content between the starting and closing A tags.

HTH
Jimmy
0
 

Author Comment

by:M_Corkish
ID: 6860613
Cool, thanks for everything... ;-) i'm gonna have a look into that function now that i know where to start.

0
 
LVL 11

Expert Comment

by:jimmy282
ID: 6860620
Thanks for the points.
0
 

Author Comment

by:M_Corkish
ID: 6860634
no worries! hey, i just checked out your site.. 8) must really enjoy helping others out. it's good to see. ;-)

0
 
LVL 37

Expert Comment

by:meverest
ID: 6862576
sorry - got my '\' and '/' mixed up! i knew i would ... ;-)

cheers.
0

Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Internal DNS Zone Issue 13 80
Problem to Eclipse 16 132
Alias for "domain to path" translation instead of VirtualHost on Apache 3 83
whm high memory usage in processes 7 104
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question