Is it possible to validate the URL provided by a user in a form by checking if the server's response is 200?
I aim to perform this validation when the user enters information in the field and when it loses focus. Due to the same origin policy, I execute the check on the server side:
Client-side code snippet:
// Test the URL when input loses focus
angular.element(document).ready(function () {
$('#target_url').focusout(function() {
if ($('#target_url').hasClass('ng-dirty')){
$http.post('/test-url', { target_url: scope.newJob.target_url }).success(function(response){
if (response === 'valid url'){
console.log('Valid URL');
}else{
console.log('URL not valid');
}
});
};
});
});
Server-side implementation:
// Route for testing URL
app.post('/test-url', function(req, res) {
request(req.body.target_url, function (err, response) {
if (response && response.statusCode === 200) {
res.send('valid url');
}else{
res.send('URL not valid');
}
})
});
The issue here is that the response is only displayed in the console when I start typing in another input box, not when I lose focus. What am I doing incorrectly?
Note: I am using angular 1.1.5 which is why I didn't utilize 'ng-focus'.