Tuesday, November 13, 2012

jquery – Javascript Regex to validate URL and Empty at the same time

I tried to write a regular expression to work with the position-absolute jQuery validation plugin to give error if the string is left empty OR its a invalid URL. It works well for the URl but doesn\’t give error if a empty string is there . Here\’s the regex



\"regex\": /^\\S$|^(http[s]?:\\/\\/){0,1}(www\\.){0,1}[a-zA-Z0-9\\.\\-]+\\.[a-zA-Z]{2,5}[\\.]{0,1}/,


Can somebody tell what\’s wrong in the regex?


EDIT:


I\’m using this plugin for jQuery validation


Demo: Go to this URL and in the URL validtion section, remove HTTP and see the error messages


http://www.position-relative.net/creation/formValidator/demos/demoValidators.html


When I add required and URL validators together, it shoots 2 errors. But with only URL

validator, it doesn\’t say that its a Invalid URL. I just need 1 error for both Empty and

Invalid URL.


https://github.com/posabsolute/jQuery-Validation-Engine


Thanks






Rating: 5 out of 5 based on 3 ratings



The post jquery – Javascript Regex to validate URL and Empty at the same time appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/jquery-javascript-regex-to-validate-url-and-empty-at-the-same-time.html

No comments:

Post a Comment