Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Alternative Solution for Validating HTML <IMG> Tag Attributes

Last post 07-07-2008, 7:33 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  07-06-2008, 9:36 PM 43802

    Alternative Solution for Validating HTML <IMG> Tag Attributes

    I'm currently validating a variant of the following HTML <img> tag we're receiving from one of our customers:

    <img src="http://www.client_website.com.au/new/images/client_logo.gif" height="251" width="285" alt=""Client Logo/>

    For tags like this, I need a regexp that will provide a match if any of the following conditions are satisfied:

    1. The height attribute is greater than 250.
    2. The height attribute is zero.
    3. An empty height attribute has been supplied, i.e. height="" or height=''.
    4. A decimalised height attribute size has been provided, e.g. height="107.7".

    I've come up with the following Regular Expression to match these requirements, having borrowed part of it from the solution Doug Drudik provided to my last question Wink:

    (<img\b[^>]*)(height=(((['"]?)((([0-9][0-9][0-9][0-9]))|([2-9][5-9][1-9])|(\d+\.\d+)|0)(['"]?))|(''|"")))([^>]*/?>)

    Beautiful, isn't it..........................  Tongue Tied

    It seems to work, but as I'm very new to writing involved expressions like this, I'm wondering if there is a simpler solution? It wouldn't surprise me if there is a more elegant way to accomplish this. Does anyone have a 'cleaner' alternative expression that would accomplish the same as this one?
  •  07-06-2008, 10:34 PM 43803 in reply to 43802

    Re: Alternative Solution for Validating HTML <IMG> Tag Attributes

    This pattern assumes that the height attribute value is enclosed in "" or '':

    <img\b[^>]*height=(['"])(?:|\d+\.\d*|\d{4,}|2(?:5[1-9]|[6-9][0-9])|[3-9]\d{2}|0)\1[^>]*>

    Possibly others would have a better pattern to offer.


  •  07-07-2008, 12:24 AM 43804 in reply to 43803

    Re: Alternative Solution for Validating HTML <IMG> Tag Attributes

    That's great. Thanks Doug.

    If anybody else can suggest another elegant alternative, then I'd still be interested in hearing it. It all helps my learning at the end of the day. Wink

  •  07-07-2008, 7:33 PM 43849 in reply to 43804

    Re: Alternative Solution for Validating HTML <IMG> Tag Attributes

    The alternative I would offer is to not try to do range checking with the regex pattern (it's not really suited for that) but to simply extract the values and then use your program to do the value checking. THis will simplify the regex pattern considerably and play each language/regex to its strengths.

    Susan 

View as RSS news feed in XML