5

I had the following url validation regex:

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

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

This works fine including cases like http://localhost:8080, but it also validates the below ones that i dont want. The above regex should not pass the below urls

 1. http://www.example..com
 2. http://.com
 3. http://.
 4. http://www.example. 

Kindly help as am a noob in regex

Second question, though not related to the question but regex is, when i validate null and undefined against the regex /^[a-z]+$/i i get true. Is this the default behavior or am i missing something over here?

Community
  • 1
  • 1
hellojava
  • 4,904
  • 9
  • 30
  • 38
  • what do you mean by fail? are they marked as valid or invalid? – Philipp Sander Aug 21 '13 at 17:57
  • check this - http://stackoverflow.com/a/8234912/1823389 – Moazzam Khan Aug 21 '13 at 17:59
  • 2
    This has been so beat to death. Please, PLEASE, search before asking questions. – coreyward Aug 21 '13 at 18:00
  • possible duplicate of [regular expression for url](http://stackoverflow.com/questions/833469/regular-expression-for-url) – coreyward Aug 21 '13 at 18:00
  • possible duplicate of [url validation regex](http://stackoverflow.com/questions/4314741/url-regex-validation?rq=1) – coreyward Aug 21 '13 at 18:00
  • possible duplicate of [Detect URLs in text with JavaScript](http://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript?lq=1) – coreyward Aug 21 '13 at 18:01
  • possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – coreyward Aug 21 '13 at 18:01
  • possible duplicate of [what's wrong in my URL validation Regex using jquery?](http://stackoverflow.com/questions/14081758/whats-wrong-in-my-url-validation-regex-using-jquery) – coreyward Aug 21 '13 at 18:02
  • @coreyward I have already explored most of the above ones you have mentioned. Of these they even allow spaces thats not acceptable in a url. SO after all this i reached to the conclusion that, it would not make sense to validate each and everything as per . http://stackoverflow.com/questions/1410311/regular-expression-for-url-validation-in-javascript/1411800#1411800. So i just want a basic validation that I have but fails for the above cases – hellojava Aug 21 '13 at 18:06
  • @hellojava That's patently false. See the “What is the best regular expression to check if a string is a valid URL?” link. I assure you they aren't allowing spaces where spaces aren't allowed. – coreyward Aug 21 '13 at 18:09
  • @PhilippSander Sorry for that. updated the question – hellojava Aug 21 '13 at 18:21
  • @MoazzamKhan allows spaces – hellojava Aug 21 '13 at 18:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35938/discussion-between-hellojava-and-coreyward) – hellojava Aug 21 '13 at 18:36
  • @coreyward unfortunately, i cant afford to have a cumbersome regex to validate IRI or all specs as there could be number of cases, so i wana go with the bare minimum specs to syntactically validate the url (as i mentioned earlier in the ref url). All the js regex mentioned in the url either allows space or fail to validate a local server like http://localhost:8080. So i eventually reached to the regex i posted in my question, but this fails in just the above cases now. – hellojava Aug 21 '13 at 18:43
  • @hellojava Look, you're talking in circles because you have no clue what you're talking about. Why is that a “cumbersome regex”? All you're doing with a regular expression is pattern matching, so what does that regular expression do aside from “syntactically validate” the URL? URLs allow spaces, just not in the protocol or host. And you are still under the impression that there isn't a regex out there that does just this…which is pigheaded and naive. – coreyward Aug 21 '13 at 21:34

3 Answers3

8

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.

Chinmay235
  • 3,236
  • 8
  • 62
  • 93
2

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>
Govinda Yadav
  • 539
  • 4
  • 14
0

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})?(\/.*)?$/
Harold Castillo
  • 2,006
  • 19
  • 23