wjdashwood
asked on
User entered URL validation
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
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 :)
Ooops, the above function is only for validating the URL
ASKER
I'm trying to get InternetCanonicalizeUrl() to work but GetLastError simply returns ERROR_INVALID_PARAMETER. Can someone help me with the useage?
Thanks,
Will
Thanks,
Will
>>ERROR_INVALID_PARAMETER. Can someone help me with the useage?
How *are* you using it?
How *are* you using it?
ASKER
LPDWORD lpdwBufferLength = NULL;
LPTSTR lpszBuffer = NULL;
LPCTSTR lpszUrl = _T("https://www.experts-exchange.com/");
InternetCanonicalizeUrl(lp szUrl,lpsz Buffer,lpd wBufferLen gth,NULL)) ;
DWORD dwLastError = GetLastError();
if (dwLastError == ERROR_INVALID_PARAMETER)
AfxMessageBox("ERROR_INVAL ID_PARAMET ER");
if (dwLastError == ERROR_INTERNET_INVALID_URL )
AfxMessageBox("ERROR_INTER NET_INVALI D_URL");
I guess I'm not delcaring or passing lpszBuffer and lpdwBufferLength correctly.
LPTSTR lpszBuffer = NULL;
LPCTSTR lpszUrl = _T("https://www.experts-exchange.com/");
InternetCanonicalizeUrl(lp
DWORD dwLastError = GetLastError();
if (dwLastError == ERROR_INVALID_PARAMETER)
AfxMessageBox("ERROR_INVAL
if (dwLastError == ERROR_INTERNET_INVALID_URL
AfxMessageBox("ERROR_INTER
I guess I'm not delcaring or passing lpszBuffer and lpdwBufferLength correctly.
>>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("https://www.experts-exchange.com/");
DWORD dwBufferLength = 2 * _tcslen ( lpszUrl); // worst case
TCHAR szBuffer [ dwBufferLength];
InternetCanonicalizeUrl(lp szUrl,szBu ffer,&dwBu fferLength ,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("https://www.experts-exchange.com/");
DWORD dwBufferLength = 2 * _tcslen ( lpszUrl); // worst case
TCHAR szBuffer [ dwBufferLength];
InternetCanonicalizeUrl(lp
ASKER
Many thanks. I'll give that a try.
ASKER
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?
>>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
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
ASKER
Well your algorithm in the other question does exactly what I want and probably much faster :)
Well, we have a proverb here about 'catching two flies with a single slap' :o)
ASKER
Cheers,
Will