简体   繁体   English

姓氏名验证

[英]LastName FirstName validation

I have a textbox which allows users to enter their name and it should be like LastName,First Name format. 我有一个文本框,允许用户输入他们的姓名,它应该类似于LastName,First Name格式。 The below JS works perfectly as expected. 下面的JS可以按预期工作。 I will call this function on OnClientClick event in the button click. 我将在按钮单击的OnClientClick事件上调用此函数。 But I have n number of textbox that needs to be validated and now I would need to replicate this function for all the textboxed repeatedly. 但是我有n个需要验证的文本框,现在我需要为所有重复的文本框复制此功能。 Is there any way to reconstruct this that can be used for all the textbox which will reduce the code quantity and pain in the neck. 是否有任何方法可以将其重构为可用于所有文本框的方式,从而减少代码量并减轻颈部疼痛。

 function onFirstNameLastName() {
        var name = /^\w{1,20}\, \w{1,20}$/ 
        var Id = document.getElementById("bodycontent_Updates");
        var flabels = document.getElementById('<%=((TextBox)Updates.FooterRow.FindControl("txtName")).ClientID %>');
        if (flabels != null) {
            if (flabels.value != "") {
                if (!name.test(flabels.value)) {
                    alert('Please enter Last Name, First Name format');
                    document.getElementById('<%=((TextBox)Updates.FooterRow.FindControl("txtName")).ClientID %>').focus();
                    return false;
                }
                else {
                    return true;
                }
            }
            else if (name.test(flabels.value)) {
                return true;
            }
            else if (flabels.value == "") {
                return true;
            }
        }
    }

The current regular expression I use will allow name like this Clark, Michael but I also need to allow names in this format too Tim Clark, Duncan Vince which my current regex doesn't support. 我使用的当前正则表达式将允许使用这样的名称,例如Clark, Michael但是我也需要允许使用这种格式的名称, Tim Clark, Duncan Vince也不支持我的当前正则表达式。

Really appreciate any suggestions. 真的很感谢任何建议。

If your all textboxes contains same data then first use ValidatorHookupControl http://msdn.microsoft.com/en-us/library/aa338815(v=vs.71).aspx to register your controls for validation and attach same JS function name which will validate data in it. 如果您所有的文本框都包含相同的数据,则请首先使用ValidatorHookupControl http://msdn.microsoft.com/zh-cn/library/aa338815(v=vs.71).aspx来注册控件以进行验证,并附加相同的JS函数名称,将验证其中的数据。

If you have different data in your textboxes then use custom validator and trigger the same on Submit click. 如果文本框中的数据不同,请使用自定义验证程序,并在“提交”单击时触发相同的数据。 In custom validator go through each text box and call required validation function. 在自定义验证器中,遍历每个文本框并调用所需的验证功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM