Total Pageviews

Sunday, November 13, 2011

GWT: JSNI function to validate URL

public native boolean isValidUrl(String url) /*-{
    var pattern = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
    return pattern.test(url);
}-*/;

2 comments:

  1. when url is only http://www then also it's display valid ....and also same for any string after "http://www.g" etc..

    ReplyDelete
  2. try this pattern: http(s)?:\/\/([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

    ReplyDelete