-1

In Parsley URL validation, few of the valid URL gets failed.

Sample URL

http://abc-ap/acde/
http://abc-test-web/~bd/test.php/login

In my URL, I don't have .com, .in, .net etc to mention the domain extension.

Ruby code

<%= f.text_field :url, value: url, id: 'chat_url',
data:{
  parsley_required: '',
  parsley_type: 'url',
  parsley_validate_url: true,
  parsley_validate_url_message: 'Error'
}
%>

JS code

window.Parsley.addValidator('validateUrlString', {
 validateString: function (_value) {
   var regex = /^(https?:\/\/)?([\da-z\-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;       
   console.log('Value='+_value);
   return !regex.test(_value);
 }
});

For further clarification - JSFiddle Link

https://jsfiddle.net/nkvmh6qr/4/

halfer
  • 19,824
  • 17
  • 99
  • 186
Rubyist
  • 6,486
  • 10
  • 51
  • 86
  • There's no question in here! And the regex in the fiddle differ from the "JS Code" part. – SamWhan Oct 27 '17 at 08:49
  • The regex in the question *would* fail the samples, since it **requires** a `.` in the host/domain part. The regex in the fiddle would *basically* match anything and is very flawed. E.g. you have the classic `A-z` error. [Check this answer](https://stackoverflow.com/a/29771926/2064981). – SamWhan Oct 27 '17 at 08:55
  • @ClasG : i tried and pasted my approach. but still no success. – Rubyist Oct 27 '17 at 10:20
  • Huh...? Have yo read my comments? 1. Where's the question? 2. Which code are you asking about - the one in the question or the one in the fiddle? – SamWhan Oct 27 '17 at 10:43
  • @ClasG - Thanks for the reply. Actually I was looking for regex only. Rest of the function is ready to test rest of the things. – Rubyist Oct 27 '17 at 10:52
  • Possible duplicate of [What is the best regular expression to check if a string is a valid URL?](https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – SamWhan Oct 27 '17 at 11:16
  • @ClasG - But I did not have any domain extension like .com, .net etc – Rubyist Oct 30 '17 at 05:28

1 Answers1

0

It's not clear what you are trying to accomplish. Parsley has builtin support for URL validation. I don't believe abc-ap nor abc-type-web are valid domain names, so the URLs you gave are invalid.

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166