Javascript URL validation regex

前端 未结 3 1059
甜味超标
甜味超标 2020-12-20 22:32

I had the following url validation regex:

/(ftp|https?)://[^ \"]+$/

This is from the ref: Regular expression for URL validation (in JavaScript)

This

相关标签:
3条回答
  • 2020-12-20 23:20

    Try this

    function is_valid_url(url)
    {
         return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
    }
    

    Call the is_valid_url(YOUR_WEBSITE_URL) function everywhere which you want to website validate.

    0 讨论(0)
  • 2020-12-20 23:20

    Try This ..

    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
    <script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
    <script>
    // just for the demos, avoids form submit
    jQuery.validator.setDefaults({
      debug: true,
      success: "valid"
    });
    $( "#myform" ).validate({
      rules: {
        field: {
          required: true,
          url: true
        }
      }
    });
    </script>

    0 讨论(0)
  • 2020-12-20 23:34

    I built this regex IP and DNS validation for javascript.

    /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?((([a-z0-9])*[:]){1}|(([a-z0-9])*[.]){1}|(([0-9]){1,3}([.]|[:])){3}(([0-9]){1,3}[:]){1})([a-z]{2,5}|[0-9]{1,5})([.]([a-z0-9]){1,3})?(\/.*)?$/
    
    0 讨论(0)
提交回复
热议问题