0

I am very new to AngularJS and currently I am working on a form which is designed in angularjs.

My task is to validate a textbox input with a list. If the value entered in textbox is not present in the list then it should throw a validation error.

I have written the below lines of code for getting the list items through rest API:

app.factory("EatonScanningFactory", ['$http', function($http) {

    var EatonScanningFactoryObj = {};
    EatonScanningFactoryObj.GetToolMaxTimeList = function (columnName) {
        return $http({

            method: 'GET',

            url: _spPageContextInfo.webAbsoluteUrl
            + "/_api/web/lists/getbytitle('Tool%20Max%20Time')/Items/"
            + "?$select=Text,Value&$orderby=Text&$filter=Title eq '" + columnName + "'",

            headers: { "Accept": "application/json;odata=verbose" }

        });
    }

It will return the list items into an array. The below lines of code are for accessing calling the above function:

    var getToolId = EatonScanningFactory.GetToolMaxTimeList('ToolNumber');
    var getMaxLife = EatonScanningFactory.GetToolMaxTimeList('MaxLife');

I am unable to proceed further as I am not sure how to validate if my text box input is available in the list or not.

Please help

4
  • Can you post a sample of your list ? what does it look like in JSON ? Commented Aug 17, 2016 at 6:32
  • List has two columns "ToolNumber" & "MaxLife". Commented Aug 17, 2016 at 6:34
  • It would be easy id you post a sample. Commented Aug 17, 2016 at 6:36
  • ToolNumber Max Life 123456 45 223365 60 Commented Aug 17, 2016 at 6:44

1 Answer 1

1

Hi there here is a plausible solution as an example:

$scope.list = EatonScanningFactory.GetToolMaxTimeList('MaxLife');

Javascript (Native) Example:

function validateList() {
    var IsInList = false;
    for (var i = 0; i < $scope.list.length; i++) {

        if ($("#textbox").val() == $scope.list[i].listvalue) {
            IsInList = true;
            break;
        } 
    }

    return IsInList;
}

Angular Example:

$scope.validateFunction = function() {  
    angular.forEach($scope.list, function(value, key){
        var IsInList = false;

        if($("#textbox").val() == value.listvalue) {
            IsInList = true;
            break;
        }
    });

    return IsInList;
}

//Invoke the function like follow:
$scope.validateFunction();
Sign up to request clarification or add additional context in comments.

3 Comments

@TheAwesomeCoder : its giving an error "length" is not a property?
Sorry for getting back only now, I had to do some stuff. The length property returns the total items in the object or array starting from 1 upwards. Try console.log(yourobject) / or from example console.log($scope.list) and see what the data looks like this will give you an indication to if your data is correctly formatted in object/array. If it is not properly formatted from the JSON response, simply use $scope.list = JSON.parse($scope.list); //this should convert it back to readable javascript oject.
can we do it through angular.foreach(var in variable).. If so please canyou please letm e know the syntax?

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.