DataLayer推送显示字段验证

时间:2019-05-29 07:19:27

标签: javascript c# razor google-analytics google-tag-manager

当前,当用户单击创建帐户按钮

时,使用 DataAnnotations 可以使字段验证按预期工作

因此,我接下来要做的是将用户单击创建帐户按钮失败的字段传递到下面验证功能下的Validation Fields

因此,如果任何字段(用户名,名字)验证失败(即用户名或名字),则验证功能会将Validation Fields替换为任何失败的字段。例如,如果用户名验证失败。因此是“验证字段”:用户名。如果两个字段均失败,则显示“验证字段”:用户名,密码。

Account.cs

[Required("Username is required")]
[StringLength(9, ErrorMessage="Must be under 9 characters")]
public string Username {get; set;}

[Required("Firstname is required")]
[StringLength(20, ErrorMessage="Must be under 20 characters")]
public string Firstname {get; set;}

Account.cshtml

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @id = "form-main" }))

            @Html.ValidationSummary(true, "", new { @class = "styled" })

             <div class="form-group">
                    @Html.LabelFor(model => model.Username)
                    @Html.TextBoxFor(model => model.Username, new { @class = "form-control", @placeholder = "Please enter here" })
                    @Html.ValidationMessageFor(model => model.Username, "", new { @class = "styled" })

                </div>

            <div class="form-group">
                    @Html.LabelFor(model => model.FirstName)
                    @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", @placeholder = "Please enter here" })
                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "styled" })

                </div>
            <button id="btnCreateAccount" type="submit" onclick="onCreateAccount()">Create Account</button>
        }



@section Scripts {
        <script type="text/javascript">
        function Validate() {
                window.dataLayer = window.dataLayer || [];
                window.dataLayer.push({
                    'event': 'Submit',
                    'Status': 'Fail',
                    'Label': 'Account Register form',
                    'Validation Fields': 'failed field validation' 
                });
            }

        function onCreateAccount()
        {
            Validate()
        }
        </script>   

}

那么如何实现上面的方案,我可以获取失败验证的字段,然后将其传递给 Validate 功能的'Validation Field'? >

0 个答案:

没有答案