Copy link to clipboard
Copied
I have a form filed and need to make sure that there is not URL's being passed through it. I would like to search the field and test it for "http" and/or "www", if those charters are present I would like to send it to a special page that handels it and then abort the rest of the script.
Can anyone help me with this?
Thanks!
Nick
See if this helps. It's from my regex cheat sheet.
<cfset regex = "^(http://|www)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$" />
Then :
#refind(regex,"your_string")#
Should return 1 if there's a match, or 0 if there isn't.
Copy link to clipboard
Copied
See if this helps. It's from my regex cheat sheet.
<cfset regex = "^(http://|www)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$" />
Then :
#refind(regex,"your_string")#
Should return 1 if there's a match, or 0 if there isn't.
Copy link to clipboard
Copied
Paul,
You were spot on with your post, I can't thank you enough for your help!
Have the code in place and it is working like a charm.
Nick
Copy link to clipboard
Copied
Paul,
After a bit more testing I noticed that your regex was not working. I had to use the following code to getthe script to work:
<cfset stringToSearch = '#form.xxx#'>
(Notice, I had to use single quotes instead of double quotes, the double quotes were killing me!)
<p>
<cfset foundit = '#FindNoCase("http",stringToSearch)#'>
<cfif foundit gt 0>
<cflocation url="http://www.xxx.com">
<cfabort>
</cfif>
Had to use:
#refind("http","your_string")#
Instead of:
#refind(regex,"your_string")#
Have no idea about Regular Expressions, wish I understood what your code is doing so that I could work on it to get it to work but all I know is that the script works this way and I will take it! 🙂
Thanks again for your help!
Nick
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Paul,
I wanted to use your code but when I uesed #refind(regex,"your_string")# and tested it it did not catch any of the test data that I used. I used the following code and it worked with all of the test data:
<cfset foundit = '#FindNoCase("http",stringToSearch)#'>
<cfif foundit gt 0>
<cflocation url="http://www.xxx.com">
<cfabort>
</cfif>
It should not matter that I used FindNoCase instead of ReFind?
If I used the following do you think it should have worked?
<cfset foundit = '#FindNoCase(regex,stringToSearch)#'>
<cfif foundit gt 0>
<cflocation url="http://www.xxx.com">
<cfabort>
</cfif>
I liked you code because it also tested for www and that would have been good to test for!
Thanks for all your help!
Nick
Copy link to clipboard
Copied
Tweaked the regex. It should find a url with or without the http://
<cfset regex = "^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$" />
<cfset my_search_string = "http://forums.adobe.com/">
<cfset foundit =refind(regex,my_search_string>
<cfif foundit gt 0>
<cflocation url="http://www.xxx.com">
<cfabort>
</cfif>
If you are passing a variable as the second argument in refind(), leave the quotes out. If it's a string, then it needs to be quoted.
Copy link to clipboard
Copied
Paul,
I will work on it tomorrow and get back to you.
Thanks again!
Nick