Solved

User entered URL validation

Posted on 2003-12-11
13
433 Views
Last Modified: 2013-11-20
Could anyone help me out with some validation code to remove all invalid characters from a string? I was hoping to find a way of using CString.Remove by specifying a range of ASCII numbers to remove. For instance, remove all ASCII chars with code less than 48. Obviously I could go through and say

strURL.Remove(‘ ‘);
strURL.Remove(‘£‘);
strURL.Remove(‘$‘);

and so on but that’s not very efficient and a lot more work! Any help much appreciated!

Cheers,
Will
0
Comment
Question by:wjdashwood
  • 6
  • 5
  • 2
13 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9921960
Why don't you use 'InternetCanonicalizeUrl()' and check the result for 'ERROR_INTERNET_INVALID_URL'?
0
 

Author Comment

by:wjdashwood
ID: 9922280
Never even heard of that before. Excellent! I'll investigate further.

Cheers,
Will
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9925737
Thatz a nice one!

You can try this also..

IsValidURL()


Syntax
=====

HRESULT IsValidURL(
    LPBC pBC,
    LPCWSTR szURL,
    DWORD dwReserved
);


Parameters
========

pBC
        It should be set to NULL.
szURL
        Address of a string value that contains the full URL to be checked.

dwReserved
        Reserved. Must be set to zero.


Return Value
=========

Returns one of the following values:

S_OK The szURL parameter contains a valid URL.
S_FALSE The szURL parameter does not contain a valid URL.
E_INVALIDARG One of the parameters is invalid.

Function Information
===============

Header Urlmon.h
Import library Urlmon.lib
Minimum availability Internet Explorer 3.0
Minimum operating systems Windows NT 4.0, Windows 95


Rosh :)
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9925746
Ooops, the above function is only for validating the URL
0
 

Author Comment

by:wjdashwood
ID: 9949979
I'm trying to get InternetCanonicalizeUrl() to work but GetLastError simply returns ERROR_INVALID_PARAMETER. Can someone help me with the useage?

Thanks,
Will
0
 
LVL 86

Expert Comment

by:jkr
ID: 9950017
>>ERROR_INVALID_PARAMETER. Can someone help me with the useage?

How *are* you using it?
0
 

Author Comment

by:wjdashwood
ID: 9950143
LPDWORD lpdwBufferLength = NULL;
LPTSTR lpszBuffer = NULL;
LPCTSTR lpszUrl = _T("http://www.experts-exchange.com/");

InternetCanonicalizeUrl(lpszUrl,lpszBuffer,lpdwBufferLength,NULL));

DWORD dwLastError = GetLastError();
if (dwLastError == ERROR_INVALID_PARAMETER)
      AfxMessageBox("ERROR_INVALID_PARAMETER");
if (dwLastError == ERROR_INTERNET_INVALID_URL)
      AfxMessageBox("ERROR_INTERNET_INVALID_URL");


I guess I'm not delcaring or passing lpszBuffer and lpdwBufferLength correctly.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9950311
>>LPDWORD lpdwBufferLength = NULL;
>>LPTSTR lpszBuffer = NULL;

That actually is the problem - the API expects a buffer to receive the output, not a NULL pointer. And, what's worese, the buffer size parameter is a null pointer also.

Make it read

LPCTSTR lpszUrl = _T("http://www.experts-exchange.com/");
DWORD dwBufferLength = 2 * _tcslen ( lpszUrl); // worst case
TCHAR szBuffer [ dwBufferLength];

InternetCanonicalizeUrl(lpszUrl,szBuffer,&dwBufferLength,NULL));


0
 

Author Comment

by:wjdashwood
ID: 9950923
Many thanks. I'll give that a try.
0
 

Author Comment

by:wjdashwood
ID: 9964829
I've got it working (I had to declare a size of the szBuffer) but the function doesn't seem to remove invalid characters. Is there a flag I need to set or something?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9966141
>>but the function doesn't seem to remove invalid characters

Hm, it should *test* for them (at least this was my intention) - removing chars is a bit dangerous, as in approx. 90% of all cases you won't end up with an existing URL
0
 

Author Comment

by:wjdashwood
ID: 9966188
Well your algorithm in the other question does exactly what I want and probably much faster :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 9966244
Well, we have a proverb here about 'catching two flies with a single slap' :o)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 117
Looking for a specific application/software 2 110
centeredAverage challenge 8 166
Bartender label printing - switch on and off graphics 3 40
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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