-1

I used type= url in html , textbox is accepting http://////////, i need it accept only two slashes. what should i do?

HTML :

<input type="url"  class="textBoxStyle" name="ServerUrl" id="TextBoxBuildServerUrl" required />
Sankar
  • 6,908
  • 2
  • 30
  • 53
nive
  • 51
  • 7
  • You might want to check this out:http://www.w3schools.com/js/js_validation.asp You can validate the input using the following regex: "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$" – anaBad Jul 19 '16 at 13:32
  • 1
    You will probably find this helpful: http://stackoverflow.com/questions/13820477/html5-input-tag-validation-for-url – κροκς Jul 19 '16 at 13:33
  • @anaBad i used ur pattern but it is rejecting my url . url is "(http://dj-5698.olw.com:8000/code)" – nive Jul 19 '16 at 13:45
  • @nive Check the answers –  Jul 19 '16 at 13:52

2 Answers2

0

Using whichever method you want, you can validate the input URL with the following regex to make sure that the user has given you a valid URL. This solution may be better long term because not only will it deny multiple slashes as expressed in your original question, but it will also reject any other invalid URL.

'^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$'

Make sure to include iu modifiers to ignore case and treat the string as UTF-16 formatting. I would put this into whatever script you're using to validate the form (i.e. a PHP POST method).

Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
0

If you are looking to go with the RegEx approach to validate your URLs, I suggest you check this gist:

var re_weburl = new RegExp(
    "^" +
    // protocol identifier
    "(?:(?:https?|ftp)://)" +
    // user:pass authentication
    "(?:\\S+(?::\\S*)?@)?" +
    "(?:" +
      // IP address exclusion
      // private & local networks
      "(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
      "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
      "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
      // IP address dotted notation octets
      // excludes loopback network 0.0.0.0
      // excludes reserved space >= 224.0.0.0
      // excludes network & broacast addresses
      // (first & last IP address of each class)
      "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
      "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
      "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
    "|" +
      // host name
      "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
      // domain name
      "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
      // TLD identifier
      "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
      // TLD may end with dot
      "\\.?" +
    ")" +
    // port number
    "(?::\\d{2,5})?" +
    // resource path
    "(?:[/?#]\\S*)?" +
    "$", "i"
);

The comments above each section of the RegEx can help you modify it to parse URLs the way you want.

κροκς
  • 592
  • 5
  • 18