简体   繁体   English

JavaScript名称姓氏验证不起作用

[英]Javascript name surname validation doesn't work

Hey guys ı want to validate name,surname etc in javascript. 嗨,我想要在javascript中验证姓名,姓氏等。

var regex = /^[A-Za-z ]{1,20}$/;

When I typed harun(which is obviously true) but it gives false,When ı typed harun12 it gives false too.Can anyone tell me what am ı doing wrong with this code? 当我输入harun(这显然是真的)但它给出了false,当我输入harun12时它也给出了false。有人可以告诉我这段代码在做什么错吗?

function check() { 函数check(){

            var name = document.getElementById('<%= txt2.ClientID%>');
            var surname = document.getElementById('<%= txt3.ClientID%>');

            var textregex = /^[A-Za-z ]{1,20}$/;
            alert(textregex.test("harun"));

            if ((name.value == '') || (textregex.test(name) == false) ) {
                $("#name_error").dialog("open");
                $("#txt2").val('');

            }
            if ((surname.value == '') || (textregex.test(surname) == false){
                $("#surname_error").dialog("open");
                $("#txt3").val('');

            }


        }

    });

        <asp:TableRow ID="TableRow2" runat="server">
            <asp:TableCell>Name:</asp:TableCell><asp:TableCell>
                <asp:TextBox ID="txt2" runat="server" />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt2" />
            </asp:TableCell>
        </asp:TableRow>

        <asp:TableRow ID="TableRow3" runat="server">
            <asp:TableCell>Surname:</asp:TableCell><asp:TableCell>
                <asp:TextBox ID="txt3" runat="server"/>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txt3" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>

As per your comment name refers to dom object not it's value so use 根据您的评论name指的是dom对象,不是它的值,请使用

alert(textregex.test(name.value)); 
//                       --^--

or 要么

var name = document.getElementById('<%= name.ClientID%>').value;
//                                                        --^--

Update 更新资料

if ((name.value == '') || (textregex.test(name.value) == false)) {
  //                                           --^--
  $("#name_error").dialog("open");
  $("#txt2").val('');
}
if ((surname.value == '') || (textregex.test(surname.value) == false)) {
  //                                                 --^--
  $("#surname_error").dialog("open");
  $("#txt3").val('');
}

Use this: 用这个:

var regex = /^[A-Za-z1-9]{1,20}$/;
regex.test('text');//True
regex.test('text123');//True

What you are wrong, it is that the validation is only sure that you have alphabetic characters, so false damage when it contains numbers. 错误的是,验证仅确保您具有字母字符,因此在包含数字时会造成错误的损坏。

If you want to allow bank space uses like this: 如果要允许使用银行空间,例如:

var regex = /^[A-Za-z1-9\s]{1,20}$/;
regex.test('text123 ');//True
regex.test('text 123');//True
regex.test('text test2');//True

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

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