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

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

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.

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)