Pages

Men

rh

8/18/2014

Jquery Validations in asp.net with C# in Button Click event

JQuery validations  for  

  • Text box, 
  •  
  • Dropdown list control , 
  •  
  • File upload control,

  • Mobile number should accept only numbers.

  • Email validations




<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"> </script>
    <script type="text/javascript">
        

$(document).ready(function () {
            $('#<%=btnSubmit.ClientID %>').click(function () {
                var FName = $('#<%=txtFName.ClientID%>').val();

                if (FName === '') {
                    alert('Please enter First Name');
                    $('#<%=txtFName.ClientID%>').focus();
                    return false;
                }
                var LName = $('#<%=txtLName.ClientID%>').val();
                if (LName === '') {
                    window.alert('Please enter LastName');
                    $('#<%=txtLName.ClientID%>').focus();
                    return false;
                }
                var DateofBirth = $('#<%=txtDob.ClientID%>').val();
                if (DateofBirth === '') {
                    alert('Please select Date of Birth');
                    $('#<%=txtDob.ClientID%>').focus();
                    return false;
                }
                var MobileNumber = $('#<%=txtMobileNo.ClientID%>').val();
                if (MobileNumber === '') {
                    alert('Please enter Mobile Number');
                    $('#<%=txtMobileNo.ClientID%>').focus();
                    return false;
                }

                var EmailId = $('#<%=txtEmailId.ClientID%>').val();
                if (EmailId === '') {
                    alert('Please enter EmailId');
                    $('#<%=txtEmailId.ClientID%>').focus();
                    return false;
                }
                if (validateEmail(EmailId)) {

                }
                else {

                    alert('Invalid Email Address');
                    e.preventDefault();
                }

                var Occupation = $('#<%=ddlOccupation.ClientID%>').val();
                if (Occupation === '----------Select----------') {
                    alert('Please select Occupation');
                    $('#<%=ddlOccupation.ClientID%>').focus();
                    return false;
                }
                var CustomerType = $('#<%=ddlCustomerType.ClientID%>').val();
                if (CustomerType === '----------Select----------') {
                    alert('Please select Customer Type');
                    $('#<%=ddlCustomerType.ClientID%>').focus();
                    return false;
                }
                var Nationality = $('#<%=ddlNationaltiy.ClientID%>').val();
                if (Nationality === '----------Select----------') {
                    alert('Please select Nationality');
                    $('#<%=ddlNationaltiy.ClientID%>').focus();
                    return false;
                }
                var Address1 = $('#<%=txtAddress.ClientID%>').val();

                if (Address1 === '') {
                    alert('Please enter Address1');
                    $('#<%=txtAddress.ClientID%>').focus();
                    return false;
                }
                var Address2 = $('#<%=txtAddress2.ClientID%>').val();
                if (Address2 === '') {
                    window.alert('Please enter Address2');
                    $('#<%=txtAddress2.ClientID%>').focus();
                    return false;
                }

                var City = $('#<%=txtTownandCity.ClientID%>').val();

                if (City === '') {
                    alert('Please enter City');
                    $('#<%=txtTownandCity.ClientID%>').focus();
                    return false;
                }
                var State = $('#<%=txtStateOrRegion.ClientID%>').val();
                if (State === '') {
                    window.alert('Please enter State');
                    $('#<%=txtStateOrRegion.ClientID%>').focus();
                    return false;
                }
                var PostalCode = $('#<%=txtPostalCode.ClientID%>').val();
                if (PostalCode === '') {
                    window.alert('Please enter Postal Code');
                    $('#<%=txtPostalCode.ClientID%>').focus();
                    return false;
                }

                var Country = $('#<%=ddlCountry.ClientID%>').val();
                if (Country === '----------Select----------') {
                    alert('Please select Country');
                    $('#<%=ddlCountry.ClientID%>').focus();
                    return false;
                }

                var AddressProof = $('#<%=ddlAddressProof.ClientID%>').val();
                if (AddressProof === '----------Select----------') {
                    alert('Please select Address Proof');
                    $('#<%=ddlAddressProof.ClientID%>').focus();
                    return false;
                }

                var AddressProofNumber = $('#<%=txtAddressProofNo.ClientID%>').val();
                if (AddressProofNumber === '') {
                    window.alert('Please enter Address Proof Number');
                    $('#<%=txtAddressProofNo.ClientID%>').focus();
                    return false;
                }


                var SourceofIncome = $('#<%=ddlSourceofIncome.ClientID%>').val();
                if (SourceofIncome === '----------Select----------') {
                    alert('Please select Source of Income');
                    $('#<%=ddlSourceofIncome.ClientID%>').focus();
                    return false;
                }

                var fileExt = $('#<%=fleAddressProof.ClientID %>').val();
                if (fileExt === '') {
                    alert('Please select a Address Proof');
                    return false;
                }
                else {
                    var fileExtension = ['jpeg', 'jpg', 'png', 'gif'];
                    if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
                        // alert("Only '.jpeg','.jpg' formats are allowed.");
                        $('#<%=btnSubmit.ClientID %>').attr("disabled", true);

                        alert('.jpeg, .jpg, .png, .gif formats are allowed');
                    }
                    else {
                        $('#<%=btnSubmit.ClientID %>').attr("disabled", false);

                    }
                }








            });



            $('#<%=fleAddressProof.ClientID %>').change(
            function () {
                var fileExtension = ['jpeg', 'jpg', 'png', 'gif'];
                if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
                    // alert("Only '.jpeg','.jpg' formats are allowed.");
                    $('#<%=btnSubmit.ClientID %>').attr("disabled", true);

                    alert('.jpeg, .jpg, .png, .gif formats are allowed');
                }
                else {
                    $('#<%=btnSubmit.ClientID %>').attr("disabled", false);

                }
            })


            $('#<%=txtMobileNo.ClientID %>').keydown(function (event) {

                if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 ||
                // Allow: Ctrl+A
            (event.keyCode == 65 && event.ctrlKey === true) ||
                // Allow: home, end, left, right
            (event.keyCode >= 35 && event.keyCode <= 39)) {
                    // let it happen, don't do anything
                    return;
                }
                else {
                    // Ensure that it is a number and stop the keypress
                    if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                        event.preventDefault();
                    }
                }
            });

            function validateEmail(sEmail) {
                var filter = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

                if (filter.test(sEmail)) {
                    return true;
                }
                else {
                    return false;
                }
            }



        });



    </script>

No comments :

Post a Comment