Skip to main content

Validating all type of controls in a form using javascript Forms Validation

Form Validation in Javascript

 

//**************Blank check validation*********************

        function check_blank(fldid, msgid) {
            var fld = document.getElementById(fldid);
            if (fld.value.length == 0 || fld.value.charAt(0) == "
") {
 
                document.getElementById(msgid).innerHTML = "The required field has not been filled in";
                //document.getElementById(error_icon_star).innerHTML = '<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML = '<b style="color:Red">*</b>';
            return true;
        }


        //**************Email formate validation*********************

        function validate_email(fieldid, msgid, error_icon_star) {
            var field = document.getElementById(fieldid);
            with (field) {
                if (check_blank(fieldid, msgid)) {
                    apos = value.indexOf("@");
                    dotpos = value.lastIndexOf(".");
                    if (apos < 1 || dotpos - apos < 2) {
                        document.getElementById(msgid).innerHTML = 'please enter correct email';
                        //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';

                        return false;
                    }
                    else {
                        document.getElementById(msgid).innerHTML = "&nbsp;";
                        //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
                        return true;
                    }
                }
                return false;
            }
        }




        //**************MobileNo 10 digit validation*********************

        function ValidateMobileNumber(fldid, ErrorMsgid) {
            var fld = document.getElementById(fldid);
            var error = "";
            var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

            if (fld.value == "") {
                error = "You didn't enter a phone number.\n";
            } else if (isNaN(stripped)) {
                error = "The phone number contains illegal characters.\n";
            } else if (!(stripped.length == 10)) {
                error = "The phone number is the wrong length. Make sure you included an area code.\n";

            }

            if (error.length > 0) {
                document.getElementById(ErrorMsgid).innerHTML = error;
                //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(ErrorMsgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
            return true;
        }





        //**************ConfirmPassword validation*********************

        function confirm_password(pwd_fieldid, conpwd_fieldid, msgid) {
            var pwd = document.getElementById(pwd_fieldid).value;
            var conpwd = document.getElementById(conpwd_fieldid).value;
            if (pwd != conpwd) {
                document.getElementById(msgid).innerHTML = 'Your passwords do not match. Please type more carefully.';
                //document.getElementById(error_icon_star).innerHTML = '<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            // document.getElementById(error_icon_star).innerHTML = '<b style="color:Red">*</b>';
            return true;
        }





        //**************Radiobuttonlist validation*********************

        function validateRadioButtonList(radiobuttonlist_id, msgid) {
            var listItemArray = document.getElementById(radiobuttonlist_id);
            var isItemChecked = false;

            for (var i = 0; i < listItemArray.rows.length; i++) {
                var listItem = document.getElementById(radiobuttonlist_id + "_" + i);
                if (listItem.checked) {
                    isItemChecked = true;
                }
            }

            if (isItemChecked == false) {
                document.getElementById(msgid).innerHTML = "Please select atleast oneitem";
                //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';

                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
            return true;
        }





        //**************dropdown validation*********************

        function Dropdown_Validation(fieldid, msgid) {
            var empty = document.getElementById(fieldid).value;
            if (empty == "--Select--") {
                document.getElementById(msgid).innerHTML = "Please Select An Item";
                //document.getElementById(error_icon_star).innerHTML='<img src="WebResource.gif" height="15px" style="width:15px;"/>';
                return false;
            }
            document.getElementById(msgid).innerHTML = "&nbsp;";
            //document.getElementById(error_icon_star).innerHTML='<b style="color:Red">*</b>';
            return true;
        }

       //******************Save button validation to all page***********

        function form_submit() {
            var fname = check_blank("<%=fname.ClientID %>", "fnamemsg");
            var lname = check_blank("<%=lname.ClientID %>", "lnamemsg");
            var username = check_blank("<%=username.ClientID %>", "usernamemsg");
            var password = check_blank("<%=password.ClientID %>", "passwordmsg");

            var confirm = confirm_password("<%=password.ClientID %>", "<%=passwordmatch.ClientID %>", "passwordmatchmsg");
            var type = validateRadioButtonList("<%=Rblist.ClientID %>", "typemsg");
            var email = validate_email("<%=email.ClientID %>", "emailmsg");
            var phone = ValidateMobileNumber("<%=phone.ClientID %>", "phonemsg");
            var country = Dropdown_Validation("<%=country.ClientID %>", "countrymsg");

            var state = check_blank("<%=state.ClientID %>", "statemsg");
            var city = check_blank("<%=city.ClientID %>", "citymsg");
            var address = check_blank("<%=address.ClientID %>", "addressmsg");

            if (fname && lname && username && password && confirm && type && email && phone && state && city && address) {
                return true;
            }
            return false;
        }

Popular posts from this blog

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview , Scrollble gridview with fixed header through javascript , File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview. We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.