locked
I need Textbox with validation first two characters should be numeric and rest of them be alphanumeric RRS feed

  • Question

  • User-94139624 posted

    so please help any one to solve this i tried many ways 

    Monday, November 27, 2017 12:29 PM

All replies

  • User2103319870 posted

    Textbox with validation first two characters should be numeric and rest of them be alphanu

    You can use Regex to enforce your required format in textbox, Please find sample implementation

    Javascript code

    function FnFirstTwoNumericValues(x) {
                    //Get the textbox value
                    var txt = document.getElementById(x).value;
                    txt = txt.substr(0, 2);
                    //your expression
                    var regex = new RegExp("^[0-9]{2}[0-9A-Za-z]{0,}$");
                    //Check if textbox value matches with regex format
                    if (!regex.test(txt)) {
                        //if not then display the message
                        alert("First two Characters should be numbers");
                    }
                }

    Attach an onblur event to textbox control to the javascript function

     <asp:TextBox ID="TextBox8" runat="server" onblur="FnFirstTwoNumericValues(this.id)"></asp:TextBox>

    Monday, November 27, 2017 2:38 PM
  • User-1838255255 posted

    Hi Saipavanchandu,

    According to your description and needs, I think you could use JS regex expression to achieve this, for more details, please check the following sample:

    Sample Code:

    <head runat="server">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
            $(function () {
                $("#TarVal").blur(function () {
                    var re = new RegExp('^[0-9]{2}[0-9a-zA-Z]+$');
                    var result = re.test($("#TarVal").val());
                    if (result == false)
                    {
                        alert("Validate Fail");
                    } else if (result == true)
                    {
                        alert("Validate Pass");
                    }
                })
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TarVal" runat="server"></asp:TextBox>
            </div>
        </form>
    </body>

    Result:

    Best Regards,

    Eric Du

    Tuesday, November 28, 2017 6:19 AM