Solved

Website URL validation

Posted on 2002-03-12
13
1,026 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Expert Comment

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

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

Author Comment

by:M_Corkish
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:jimmy282
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points.
0
 

Author Comment

by:M_Corkish
Comment Utility
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
Comment Utility
sorry - got my '\' and '/' mixed up! i knew i would ... ;-)

cheers.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now