Friday, 26 September 2014

Javascript to validate Textbox and Dropdownlist on submit in ASP .Net

<script type="text/javascript">
        var TextBoxItemName = '<%=TextBoxItemName.ClientID %>';
        var TextBoxPurchasePrice = '<%=TextBoxPurchasePrice.ClientID%>';
        var TextBoxSalePrice = '<%=TextBoxSalePrice.ClientID%>';
        var DropDownListUnit = '<%=DropDownListUnit.ClientID%>';
        var TextBoxDescription = '<%=TextBoxDescription.ClientID%>';   


        function Save() {
           
            if (document.getElementById(TextBoxItemName).value == "") {
                alert('Please enter item name.');
                document.getElementById(TextBoxItemName).focus();
                return false;
            }
            if (document.getElementById(DropDownListUnit).value == 0) {
                alert('Please select unit.');
                document.getElementById(DropDownListUnit).focus();
                return false;
            }           
            if (document.getElementById(TextBoxPurchasePrice).value == "") {
                alert('Please enter purchase price.');
                return false;
            }
            if (ex.test(document.getElementById(TextBoxPurchasePrice).value) == false) {
                alert('Incorrect purchase price');
                document.getElementById(TextBoxPurchasePrice).focus();
                return false;
            }
            if (document.getElementById(TextBoxSalePrice).value == "") {
                alert('Please enter sale price.');
                return false;
            }
            if (ex.test(document.getElementById(TextBoxSalePrice).value) == false) {
                alert('Incorrect sale price');
                document.getElementById(TextBoxSalePrice).focus();
                return false;
            }
        }     
      
    </script>

No comments:

Post a Comment