Check for url anchor code in a string with JavaScript

Well, I have come across the need to make sure that a string doesn’t contain any code that would make it a link. For example, I need to allow the user to enter a filename but not have any html code in it.

For this current situation:
<a href=”myfile.html”> is an invalid string.
http://myfile.html        is an invalid string.

I want the user to only be able to submit “myfile.html” and then I will handle the linking and referencing using server side code.
[code] function checkurl(){ // function to ensure no a href or quotes get in here.
console.log(“Lets check the url to make sure no a href or quotes”);
var thelink=$(“#slide_external_link”).val();
if(thelink !== “”){ // check for stuff we dont want.
if(thelink.match(/href=”([^”]*”)/g) || /^(f|ht)tps?:///i.test(thelink)){ // this looks like it comments out after i.test, but its workin.
console.log(“Can’t process the request because there is code inside the string. We only want the address of page without anchor code”);
alert(“Please remove any anchor code from the external link. An example of valid entry is: directory.asp_Q_pagenumber_E_520”);
return false; } } } // end check url funk[/code]
Now I can call this function as part of my client side validation upon form submit. I also need to implement a server side catch as well, just in case.

Leave a Reply