0

I have a text input that pulls the value of the window.location.href, looks for ?= and strips out the trailing value which is used as the input value. I can't get the validation to check if there is a value after the =, and display an error if there isn't.

The full url it is testing is http://localhost:3000/admin/tagger.html?=1234567, where 1234567 should be the value in the textbox. If there is no numeric value after the =, show an error.

var myApp = angular.module("myApp", []);

myApp.controller("myController", function ($scope, $http) {

$scope.init = function () {
    var url = window.location.href;
    var accountId = url.substr(url.indexOf("=") + 1);
    if ($(".accountNumber").val() !== window.location.href + "?=") {
        alert("ERROR");
    } else {
        $scope.accountNumber = accountId;
    }
}

});

JSFIDDLE

2 Answers 2

1

If I understood you questions correctly then you can change your code like this.

$scope.init = function () {
    var url = window.location.href;
    var urlParts = url.split('?=');
    var accountId = urlParts[1];

    if (!accountId) {
        alert("ERROR");
    } else {
        $scope.accountNumber = accountId;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, but getting the error even when the value is correct: jsfiddle.net/mattography/sLew9ygp/3
This is closer, just need to check that the URL is also not just http://localhost:3000/admin/tagger.html?, but how to do it so that I only have to check that it has ?=a-number, and not everything before it. In other words, how do I ensure that it doesn't validate true if the URL just contains http://localhost:3000/admin/tagger.html?
This update is not allowing me to grab the value of the account number input for use in my JSON form post, if the field is disabled: jsfiddle.net/mattography/sLew9ygp/6
0

If you configure your routing differently, you can for example change the url to: http://localhost:3000/admin/tagger/1234567

Your routing would look something like this:

myApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/admin/tagger/:id', {
         templateUrl: '<path to your html template>'
        }
    });
}]);

And then in your function you can use $routeParams to check the value of :id like this:

myApp.controller('myController', ['$routeParams', function($routeParams) {
     $scope.init = function() {
        if ($(".accountNumber").val() !== $routeParams.id) {
            alert("ERROR");
        } else {
             $scope.accountNumber = accountId;
        }
}])

Or something along these lines. The principle is: you can get the number that you want to check by calling $routeParams.id (or $routeParams['id']).

1 Comment

I tried this, but the structure of the URL is being sent as ?id=some-number. I tried to reconfigure it, but it isn't working: jsfiddle.net/mattography/sLew9ygp/2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.