Solved

Website URL validation

Posted on 2002-03-12
13
1,028 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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