0

I want a function in html5 to validate web address textfield. This is the code i tried. Its nt working.

function validateURL(textval) {
      var urlregex = new RegExp(
            "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
      return urlregex.test(textval);
    }
MPelletier
  • 15,673
  • 14
  • 84
  • 131

2 Answers2

0

How about this ?

Added the pattern="https?://.+" attribute to input field, you have improved your url validation by demanding that all url's start with either http:// or https://

<input type="url" name="url" required = "required" value="" pattern="https?://.+" >

Demo: http://jsfiddle.net/Stk7s/

Krish R
  • 22,188
  • 7
  • 49
  • 57
0

by default this will validate against the data given

     <input type="url" name="url" required = "required" value="" >
Karthick Kumar
  • 2,360
  • 1
  • 15
  • 29