SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating an image URL with javascript

    Can someone help me with a javascript code to validate an image URL?

    I want to validate the following:

    1. the URL must start with http://
    2. the URL must end with .gif, .jpg, .jpeg or .png

    I'm I leaving out something?
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var url_ = "your url";

    if(url_.match(/http\:\/\/.*?gif)) alert("ok");

    te samo for jpg or png
    cheers

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) What if someone wanted to point to an FTP URL?
    2) Those are fine if you only want to allow those 3 formats. I've done the same thing myself.

    reminder, you've got at least one error there -- you're missing the slash that would indicate the end of the RegExp.

    The RegExp test() method is better for this.

    Here's how I might do it:
    Code:
    if(/^http:\/\/.+\.(gif|png|jpg|jpeg)$/i.test(url_))
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •