0

I added the knockout validation however it seems its throwing everything I put in the field: a,1,?, etc -- only thing works is having the field empty. I'm using the ko validation library and am extending it by using the pattern rule.

The ko template:

<script type="text/html" id="solutionRowTemplate">
                    <tr>
                        <td>
                            <input type="text" class="whole" data-bind="value: firstWhole, valueUpdate: 'afterkeydown'" />
                        </td>
</tr>
</script>

The ko viewmodel:

<script type="text/javascript">
    var solutionData = @Html.Raw(new JavaScriptSerializer().Serialize(Model.SolutionList));

    function SolutionModel(firstWhole) {
        this.firstWhole = ko.observable(firstWhole);
    }

    var viewModel = {
        solutions: ko.observableArray(ko.utils.arrayMap(solutionData, function (item) {
            var model = new SolutionModel(item.FirstWhole);
            model.firstWhole.extend({ pattern: '^[a-z0-9].$' });
            return model;
        })),
        addSolution: function () {
            this.solutions.push(new SolutionModel('', '', '', '', '', '', '', '', '', '', '', ''));
        },
    };

    ko.applyBindings(viewModel);
</script>

Won't be surprised if its wrong on how I'm adding the extend...

Thanks, -rob

Rob Koch
  • 1,523
  • 3
  • 18
  • 26

1 Answers1

1

It should work. But values you tried - not. 'a', '1' and '?' is not valid value for '^[a-z0-9].$' pattern. For this pattern only values where first symbol is letter(a-z) or digit and second symbol is any will be valid. For example: '1a', 'b#', 'Ab'.

Artem Vyshniakov
  • 16,355
  • 3
  • 43
  • 47
  • d-oh, a regex problem and not a ko validation problem! Guess I just needed another set of eyes. Got rid of the dot, and now it accepts what I needed -- using this pattern now: '^[0-9+]|\\?$' (any number and question mark only) – Rob Koch Oct 11 '12 at 01:23