Textbox max length validation using javascript in ASP.NET

Textbox max length validation using javascript

In this example i have explained how to do textbox max length validation using javascript. here i used to validate regular expression. this textbox will allow max 10 characters only. Using JQuery also we can do text box count validation.

<html>
<head>
    <title></title>
    <script type="text/javascript">
        function validate() {
            var txtMaxLength = document.getElementById("txtMaxLength").value;

            //Max length validation with regular expression
            var retxtMaxLength = /^[\s\S]{0,10}$/;
            if (!txtMaxLength.match(retxtMaxLength)) {
                alert('Max length only 10 characters.');
                return false;
            }
        } 
    </script>
</head>
<body>
<input type="text" id="txtMaxLength" /><br />
<input type="button" value="Max Length Validation" onclick=" return validate();" />
</body>
</html>

Before doing this we may require text box required field validation.
For more form validation you can go see Form Validation.
See more JQuery Interview Questions


Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar