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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.