Derek Hohls wrote:
> I need to check that a valid web address (grammatically correct
> anyway) is being entered in a form field.  I am trying to check for
> the address starting with a valid prefix - the regexp I am using is:
>
> pattern="^(http|https|ftp)://"

The regexp validator anchors the pattern by default, as if you had
^ and $ at the ends of the pattern.

For reference, here is a very strict field that only accepts a valid,
non-numeric http URL, without any custom port, username, password or
query string, and checks that it actually resolves and points to an
existing, public web page.

You might want to loosen some of the requirements, depending on your
application :-)


<field id="url" required="true">
  <datatype base="string"/>
  <label>URL:</label>
  <initial-value>http://</initial-value>
  <validation>
    <regexp pattern="http://(?:[a-z0-9-]+\.)+[a-z]{2,}(?:/[^?]*)?">
      <failmessage>Please insert the URL of a web page</failmessage>
    </regexp>
    <javascript>
      <!-- check that the URL exists -->
      try {
        var conn = new java.net.URL(widget.getValue()).openConnection()
        conn.setInstanceFollowRedirects(true)
        conn.setConnectTimeout(10000)
        conn.setReadTimeout(5000)
        conn.connect()
        if (conn.getResponseCode() != 200) throw 'error'
      } catch (e) {
        widget.setValidationError(
            new ValidationError("Please insert an existing URL", false))
        return false
      }
      return true
    </javascript>
  </validation>
</field>


HTH
Tobia

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to