How to use CompareValidator to check that the text does not contain any number

1.8k views Asked by At

I try to validate the name and surname. I need to check if the user's input is without numeric character but when I run the code, no error message appear. Here's my code:

            <asp:CompareValidator ID="CompareValidator2" runat="server" 
                ControlToValidate="TextBox3" ErrorMessage="ชื่อต้องเป็นตัวอักษรเท่านั้น" 
                ForeColor="Red" Operator="DataTypeCheck" Type="String" ValidationGroup="Group1">*</asp:CompareValidator>
            <asp:CompareValidator ID="CompareValidator3" runat="server" 
                ControlToValidate="TextBox6" ErrorMessage="นามสกุลต้องเป็นตัวอักษรเท่านั้น" 
                ForeColor="Red" Operator="DataTypeCheck" Type="String" ValidationGroup="Group1">*</asp:CompareValidator>

Any help appreciated.

2

There are 2 answers

2
Justin Iurman On BEST ANSWER

Try to use RegexExpressionValidator in addition to CompareValidator

<asp:RegularExpressionValidator ID="ValidName" runat="server" ControlToValidate="Name" Display="Dynamic" ValidationGroup="Group1" ValidationExpression="^[^0-9]+$">syntax error...</asp:RegularExpressionValidator>
2
gwt On

You could use a CustomValidator instead , as follows :

<asp:CustomValidator ID="NoneNumeric" runat="server" ControlToValidate="txtNoneNumeric" ClientValidationFunction="IsNoneNumeric">Enter Only None Numeric Values !</asp:CustomValidator>

and write a javascript function as follows :

    <script type="text/javascript">
    function IsNoneNumeric(sender, arguments) {
        var NoneNumeric = true;
        for (count = arguments.Value.length; count > 0; count--) {
            TempChar = arguments.Value.substring(count, count + 1);
            if (numString.indexOf(TempChar, 0) != -1) {
                NoneNumeric = false;
            }
            if (NoneNumeric == true) {
                arguments.IsValid = true;
            }
            else {
                arguments.IsValid = false;
            }
        }
    }
</script>