Here is the URL expression(regex) I am currently using to validate website URLs :
/(https?:\/\/)(www)?[A-Za-z0-9.\-@_~]+\.[A-Za-z]{2,}(:[0-9]{2,5})?(\/[A-Za-z0-9\/_\-.~?&=]*)*/
My implementation of angular JS code looks like this :
<md-input-container class="md-block" style="margin-top:20px;">
<label>Website</label>
<input ng-model="schoolDetails.website" name="website" ng-change="editField()" type="url" ng-pattern="/(https?:\/\/)(www)?[A-Za-z0-9.\-@_~]+\.[A-Za-z]{2,}(:[0-9]{2,5})?(\/[A-Za-z0-9\/_\-.~?&=]*)*/">
<div ng-messages="schoolDetailsForm.website.$error">
<div ng-message="pattern">Please enter a valid website</div>
</div>
</md-input-container>
If I provide a valid URL http://www.bharatividyapeeth.edu
, it works properly.
However, when entering an invalid URL such as http://www.
, the error message appears. But for an invalid URL like http://www.bharatividyapeeth
, no error message shows up and it accepts it as a valid URL.
Could someone please help me correct my code to ensure proper validation of website URLs?
Thank you.