javascript - detect if string is a valid URL and is not empty -
this question has answer here:
- check if javascript string url 9 answers
i check if 1 of input fields empty , if #urlink format of url before perform function in js:
$scope.favurls.$add
not sure how go it.
html
<input type="text" id="urlname" class="form-control" placeholder="" ng-model="mvname" /> <input type="text" id="urllink" class="form-control" placeholder="" ng-model="mvurl" />
app.js
$scope.savetolist = function(event) { var mvname = $scope.mvname.trim(); var mvurl = $scope.mvurl.trim(); if (mvname.length > 0) { $scope.favurls.$add({ name: mvname, title: mvurl }); urlname.value = ''; //urlname id of input box - angular rocks! urllink.value = ''; //urlname id of input box - angular rocks! } }
use following function validate url
function validurl(str) { var pattern = new regexp('^(https?:\/\/)?'+ // protocol '((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|'+ // domain name '((\d{1,3}\.){3}\d{1,3}))'+ // or ip (v4) address '(\:\d+)?(\/[-a-z\d%_.~+]*)*'+ // port , path '(\?[;&a-z\d%_.~+=-]*)?'+ // query string '(\#[-a-z\d_]*)?$','i'); // fragment locater if(!pattern.test(str)) { alert("please enter valid url."); return false; } else { return true; } }
Comments
Post a Comment