Jump to content

Test for URL & FILE


_tina_

Recommended Posts

Hi,

 

I have two functions.  One checks if a string is a URL an the other checks for certain file types.

The issue with the one checking for a URL is that, it also thinks that a file is a URL.

	function isValidURL(url){ 
	    var RegExp = /^(([\w]+?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
	    if(RegExp.test(url)){ 
	        return true; 
	    }else{ 
	        return false; 
	    } 
	}
  

I nee this to check if it's a URL and ignore files.

 

Then this one to check for files, it's basically just not working, can anyone see where I'm going wrong on this one?

	function checkExt(e) { 
		   value=e.value;
		if( !value.match(/\.(doc)|(jpg)|||(pdf)$/) ){ 
			return true; 
		}
		else { 
			return false;
		}
	}

 

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/181529-test-for-url-file/
Share on other sites

if( !value.match(/\.(doc|jpg|pdf)$/) ){

 

that will only check for those 3 file types though..

 

Thats what I thought but when I pass in x.pdf, x.doc or x.jpg it doesn't recognize them as being of that file type. 

 

My main problem though is the first one, is there a way you know of to make it ignore files and just test for URL's?  I can't seem to get the regular expression correct.

Link to comment
https://forums.phpfreaks.com/topic/181529-test-for-url-file/#findComment-958043
Share on other sites

Thank you for that.  How exactly would I do that?  My JavaScript isn't very good.

 

Thanks again.

 

hmm actually on 2nd look that is gonna evaluate true every time there is a dot in there somewhere.  You will have to assign value.match(..) to a var and then check if variable[1] exists

Link to comment
https://forums.phpfreaks.com/topic/181529-test-for-url-file/#findComment-958050
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.