SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tet for URL - Not file

    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.
    Code:
    		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?
    Code:
    		function checkExt(e) { 
    			   value=e.value;
    			if( !value.match(/\.(doc)|(jpg)|||(pdf)$/) ){ 
    				return true; 
    			}
    			else { 
    				return false;
    			}
    		}
    Thanks in advance

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    How is it supposed to distinguish between the folder

    http://www.example.com/notafile.gif

    and the file

    http:www.example.com/isafile.gif

    unless of course the folder reference also has a filename on the end

    http://www.example.com/notafile.gif/isafile.gif

    assuming that isn't another folder name on the end.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea, that's why I posted here in hope someone might know how to do this.

    I have a field, the user can either upload a file or enter a url. I just need to be able to distinguish between the two.

    Thanks for your reply.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Since a URL would be entered in a type-text" input field while a file to be uploaded needs to be input in a type="file" input field can't you distinguish them that way?

    Once the file is uploaded then it is accessed via a URL and so there is no way to distinguish past that point.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, because, the user chooses from a select menu whether it will be a URL or a file, and based upon that, they either get a file field or a text field. Whatever they choose, JQuery makes that field available to them.

    Quote Originally Posted by felgall View Post
    Since a URL would be entered in a type-text" input field while a file to be uploaded needs to be input in a type="file" input field can't you distinguish them that way?

    Once the file is uploaded then it is accessed via a URL and so there is no way to distinguish past that point.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    In that case you just need to test what is selected from the select list to know which it will be.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    In that case you just need to test what is selected from the select list to know which it will be.
    I can't do it like that. When they select file or url, either a file field or a text field is dynamically displayed. This is an edit page and the values are read in from the db. So, if they had chosen file, I need to be able to know this before the form is generated so I can either display the file field or the url field.

    So basically, I just need to correct the functions below in my first post so that they will detect a file or a url.
    My url function, that looks like it's going to return true all the time.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you need to know beforehand then you can't do it in JavaScript since JavaScript only runs after the page has loaded.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •